我的一个实体中有 ManyToOne 关系,如下所示:
class License {
// ...
/**
* Customer who owns the license
*
* @var \ISE\LicenseManagerBundle\Entity\Customer
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="licenses")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
// ...
}
class Customer {
// ...
/**
* Licenses that were at one point generated for the customer
*
* @var \Doctrine\Common\Collections\ArrayCollection
* @ORM\OneToMany(targetEntity="License", mappedBy="customer")
*/
private $licenses;
// ...
}
这会生成一个数据库架构,其中许可表的“customer_id”字段允许为空,这正是我不希望的。
这是我创建记录以证明它确实允许引用字段为空值的一些代码:
$em = $this->get('doctrine')->getEntityManager();
$license = new License();
// Set some fields - not the reference fields though
$license->setValidUntil(new \DateTime("2012-12-31"));
$license->setCreatedAt(new \DateTime());
// Persist the object
$em->persist($license);
$em->flush();
基本上,我不希望在没有分配客户的情况下保留许可证。是否需要设置一些注释,或者我应该只需要将客户对象传递给我的许可证的构造函数?
我使用的数据库引擎是 MySQL v5.1,我在 Symfony2 应用程序中使用 Doctrine 2。
最佳答案
在JoinColumn注解中添加nullable = false:
@ORM\JoinColumn(..., nullable=false)
关于php - Doctrine 2 - 多对一关系的外键不允许空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6136601/
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我正在使用DMOZ的listofurltopics,其中包含一些具有包含下划线的主机名的url。例如:608609TheOuterHeaven610InformationandimagegalleryofMcFarlane'sactionfiguresforTrigun,Akira,TenchiMuyoandotherJapaneseSci-Fianimations.611Top/Arts/Animation/Anime/Collectibles/Models_and_Figures/Action_Figures612虽然此url可以在网络浏览器中使用(或者至少在我的浏览器中可以使用:
我读过这个:Let’sstartwithasimpleRubyprogram.We’llwriteamethodthatreturnsacheery,personalizedgreeting.defsay_goodnight(name)result="Goodnight,"+namereturnresultend我的理解是,方法是定义在类中的函数或子程序,可以关联到类(类方法)或对象(实例方法)。那么,如果它不是在类中定义的,怎么可能是方法呢? 最佳答案 当你在Ruby中以这种方式在全局范围内定义一个函数时,它在技术上变成了Obje
我使用脚手架和Rails3创建了2个模型。模型是位置和作业,每个作业都有一个位置。我在脚手架生成代码中创建了所需的引用调用,但是当我查看创建新作业的View时,我看到的只是一个文本框,我应该在其中添加location_id。我怎样才能让它变成下拉菜单以获得更好的用户体验? 最佳答案 想象一下,您有每个位置的titleAPI:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select 关于r
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
在DavidFlanagan的TheRubyProgrammingLanguage中;松本幸弘theystatethatthevariableprefixes($,@,@@)areonepricewepayforbeingabletoomitparenthesesaroundmethodinvocations.谁可以给我解释一下这个? 最佳答案 这是我不成熟的意见。如果我错了,请纠正我。假设实例变量没有@前缀,那么我们如何声明一个实例变量?classMyClassdefinitialize#Herefooisaninstanceva
我有以下模型用户has_many:users_contactshas_many:contacts,through::users_contactsaccepts_nested_attributes_for:contacts,allow_destroy:true联系方式has_many:users_contactshas_many:users,through::users_contactsaccepts_nested_attributes_for:users_contacts,allow_destroy:true用户联系belongs_to:usersbelongs_to:contacts
我已经设法制作了一个仅用于注册和登录的应用程序。目前,我允许用户通过邮件帐户激活(按照本教程:https://www.railstutorial.org/book/account_activation_password_reset和“railsgeneratecontrollerAccountActivations--no-test-framework')但我希望管理员能够激活或停用用户。在我的用户模型中,我设法定义了两种方法:defactivate_account!update_attribute:is_active,trueenddefdeactivate_account!upda
我正在制作一个应用程序,我需要用户使用所见即所得的编辑器输入描述。我不能信任用户输入,所以我只需要允许a、em、ul、li标签。我如何轻松剥离其他的? 最佳答案 https://github.com/rgrove/sanitize/ 关于ruby-on-rails-允许用户只输入特定的标签,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7390075/
如何在我的模型中验证:title以便只接受字母a-z、A-z和0-9?validates:title,:format=>{with:REGULAREXPRESSION,:message=>'nospecialcharacters,onlylettersandnumbers'}正则表达式应该是什么? 最佳答案 正则表达式为/^[a-zA-Z0-9]*$/您基本上定义了三个允许的符号范围,首先是a-z,然后是A-Z,最后是0-9。最后的星号定义需要匹配零个或多个前面所述的字符,这意味着允许使用空标题。如果您需要至少一个字符,请使用+而不