开发者

CakePHP MySQL virtual field problem

I'm trying to order my results in a slightly complicated way. I've created a virtualfield called 'priority' to order results by. Here's my code:

    public $virtualFields = array(
    'priority'=>'(Venue.featured * 5000) + (Venue.views * 2) + ((Venue.image_count > 0) * 500)',
);

Now this always returns just the value of ((Venue.image_count > 0) * 500), ie either 500 or 0. If I remove that part of the field, leaving:

public $virtualFields = array(
    'priority'开发者_如何学编程=>'(Venue.featured * 5000) + (Venue.views * 2)',
);

The expected result is given. I also wish to add another component to the field, which has the same effect. Code:

public $virtualFields = array(
    'priority'=>'(Venue.featured * 5000) + (Venue.views * 2) + ((CHAR_LENGTH(Venue.blurb) > 0) * 500)',
);

In this case, again, the field is only ever 500 or 0, not the full value of all the fields.

I don't understand what's going on! If I create a virtual field that either measure image_count or CHAR_LENGTH(Venue.blurb), it works fine, but adding either of these to the other parts of the field make it fail.


The problem is that the syntax for both ((Venue.image_count > 0) * 500) and ((CHAR_LENGTH(Venue.blurb) > 0) * 500) are incorrect.

You should use an IF statement:

IF(Venue.image_count > 0, 500, 0)
IF(CHAR_LENGTH(Venue.blurb) > 0, 500, 0);

For example:

public $virtualFields = array(
  'priority' => '(Venue.featured * 5000) + (Venue.views * 2) + IF(Venue.image_count > 0, 500, 0)',
);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜