【发布时间】:2014-01-22 18:15:59
【问题描述】:
我在 Yii 中进行复杂的 AR 搜索时遇到问题,您能帮帮我吗?
我正在做这样的 AR 搜索:
$property = Properties::model()->with(
$this->propertiesConditions($photoid, $languageApp, $value->propertyid)
)->findAll('t.id = :id', array('id'=>$value->propertyid));
$this->propertiesConditions 是一个函数,它将返回一个包含搜索所需条件的数组(我不知道为什么它看起来没有正确的间距)。
public function propertiesConditions($photoid, $languageApp, $propertyid)
{
if (!is_null($photoid)) {
return array(
'owner',
'amenities',
'i18npropertiesinfos'=>array('condition'=>'i18npropertiesinfos.iso6391code="'.$languageApp.'"'),
'rates'=>array('condition'=>'rates.propertyid="'.$propertyid.'"', 'order'=>'price'),
'images'=>array('condition'=>'images.id="'.$photoid.'"'),
);
}else{
return array(
'owner',
'amenities',
'i18npropertiesinfos'=>array('condition'=>'i18npropertiesinfos.iso6391code="'.$languageApp.'"'),
'rates'=>array('condition'=>'rates.propertyid="'.$propertyid.'"', 'order'=>'price'),
);
}
}
由于某种原因,如果属性没有 $photoid ($photoid == null),它会查找正确的数组(没有“图像”的数组),但是当我将它发送到视图并 var 转储属性我得到没有'images'属性的对象(这很好),但是当我 var_dump($property->images) 我得到一个图像模型(我不知道它从哪里得到它)
谢谢。
编辑(按要求添加 var_dumps):
带有图片的属性:
object(Properties)[122]
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=13)
'id' => string '1' (length=1)
'slug' => string 'beach-house-123123-321321' (length=25)
'title' => string 'Beach House' (length=11)
'latitude' => string '123123' (length=6)
'longitude' => string '321321' (length=6)
'status' => string '0' (length=1)
'ownerid' => string '1' (length=1)
'videoURL' => string '' (length=0)
'numberbedrooms' => null
'numberbathrooms' => null
'imagesOrder' => string '["1"]' (length=5)
'sleeps' => string '2' (length=1)
'featured' => string '1' (length=1)
private '_related' (CActiveRecord) =>
array (size=5)
'owner' =>
object(owners)[123]
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=5)
...
private '_related' (CActiveRecord) =>
array (size=0)
...
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '1' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
...
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
'amenities' =>
array (size=3)
0 =>
object(amenities)[124]
...
1 =>
object(amenities)[128]
...
2 =>
object(amenities)[129]
...
'i18npropertiesinfos' =>
array (size=1)
0 =>
object(i18npropertiesinfo)[125]
...
'rates' =>
array (size=2)
0 =>
object(rates)[126]
...
1 =>
object(rates)[130]
...
'images' =>
array (size=1)
0 =>
object(images)[127]
...
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '1' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
empty
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
没有图片的属性:
object(Properties)[120]
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=13)
'id' => string '2' (length=1)
'slug' => string '' (length=0)
'title' => string 'Maya House' (length=10)
'latitude' => string '123123' (length=6)
'longitude' => string '123123' (length=6)
'status' => string '0' (length=1)
'ownerid' => string '1' (length=1)
'videoURL' => string '' (length=0)
'numberbedrooms' => null
'numberbathrooms' => null
'imagesOrder' => string '[]' (length=2)
'sleeps' => string '2' (length=1)
'featured' => string '1' (length=1)
private '_related' (CActiveRecord) =>
array (size=4)
'owner' =>
object(owners)[121]
private '_new' (CActiveRecord) => boolean false
private '_attributes' (CActiveRecord) =>
array (size=5)
...
private '_related' (CActiveRecord) =>
array (size=0)
...
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '1' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
...
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
'amenities' =>
array (size=3)
0 =>
object(amenities)[131]
...
1 =>
object(amenities)[134]
...
2 =>
object(amenities)[135]
...
'i18npropertiesinfos' =>
array (size=1)
0 =>
object(i18npropertiesinfo)[132]
...
'rates' =>
array (size=1)
0 =>
object(rates)[133]
...
private '_c' (CActiveRecord) => null
private '_pk' (CActiveRecord) => string '2' (length=1)
private '_alias' (CActiveRecord) => string 't' (length=1)
private '_errors' (CModel) =>
array (size=0)
empty
private '_validators' (CModel) => null
private '_scenario' (CModel) => string 'update' (length=6)
private '_e' (CComponent) => null
private '_m' (CComponent) => null
【问题讨论】:
-
显示两种情况的var_dump结果
-
添加了请求的 var_dump
-
如果你设置 $property = null;然后是搜索
-
我应该在哪里设置属性null?
-
在这部分之前 $property = Properties.....
标签: php activerecord yii