开发者

Require One of a Set of Fields in SQL

I have an entity, let's call 'machine', which has a group of entities fr开发者_开发问答om which it takes its name. At least one of these fields is required, but more than one can be set. So in the example below, if the host name and IP is not set, then the service tag should be. I am using Propel 1.5 as an ORM.

machine:
  id: ~
  hostname:    {type: varchar(255)}
  ip:          {type: varchar(255)}
  service_tag: {type: varchar(255)}

An article on multi-column constraints makes me wonder if it would be possible to add something like required(hostname, mac, service_tag). If this is the case, how would I accomplish this in .yml form for Propel?


At least one of these [three] fields is required

You might concatenate the three, first converting null to empty string:

           CONCAT(   ifnull(host,'') , ifnull(ip,'') , ifnull(tag,'')  )

and then this constraint:

         length(   CONCAT(   ifnull(host,'') , ifnull(ip,'') , ifnull(tag,'')  )  ) > 0

if mySQL supported check constraints.


Consider this query:

SELECT *
  FROM machine
 WHERE hostname IS NULL
       AND ip IS NULL
       AND service_tag IS NULL;

If this resultset is not the empty set then the business rule has been violated. Therefore, create a constraint or trigger that tests this.


I think its not possible with symfony and propel. But you can set up your symfony form that it requires one of the three columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜