我在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),它会查找正确的数组(没有'images'的数组)但是当我将它发送到视图并且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
答案 0 :(得分:1)
在find方法中,您选择Properties
没有images
关系的列表,但这并不意味着所有属性记录都与图像没有任何关系。如果其中一些与其他图像记录有关系,则此行$property->images
仍可以正常工作。
我不确定你的观点是什么,但是如果你打算将两个案例分开,只讨论属性列表是否有图像,你可以在属性模型中添加更多条件,以防没有'图像' 。 例如:
'notHasImages' => array(self::HAS_ONE, 'Images', array('id'=>'product_id'),
'condition'=>'images.product_id IS NULL'),
(假设product_id是Images
表上的属性的外键)(ONE到MANY)