【问题标题】:Yii - Yii gets information out of scopeYii - Yii 获取超出范围的信息
【发布时间】: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


【解决方案1】:

在您的查找方法中,您选择了Properties 列表而没有images 关系,这并不意味着所有属性记录都与图像没有任何关系。如果其中一些与其他图像记录有关系,则此行$property->images 仍然可以工作。

我不确定您的意思是什么,但是如果您打算仅将两个案例分开关于是否有图像的属性列表,您可以在没有“图像”的情况下在属性模型中添加更多条件. 示例:

'notHasImages' => array(self::HAS_ONE, 'Images', array('id'=>'product_id'),
                        'condition'=>'images.product_id IS NULL'),

(假设product_id是Images表属性的外键)(ONE to MANY)

【讨论】:

  • 您好!感谢您的回复,属性表中有“imagesOrder”列,该列有一个 JSONStringified,所有图像的 id 都以正确的顺序排列。现在,当一个属性没有任何图像(或者它有但图像被删除)时,字符串是“[]”,这就是为什么我做所有这些事情来检查它是否为空但没关系如果它是空的...我得到第二个属性的图像存在,但我不知道它来自哪里。
  • 我的上一条评论是否有助于您全面了解我的情况?
  • 有些事情让你感到困惑。举个例子:property id = 1有2张图片,为了满足你的条件组合(在propertiesConditions中),property记录中的图片数组可能是空的,但最终,当你调用语句“property- >images" 对于属性 id=1,它会显示属性有什么。如果图像 id = null,则结果不会为您提供图像数组,即使该属性可能具有,因为条件的第二个组合不包含“图像”关系
  • 你是对的,解决方案是将没有任何图像的属性设置为null。谢谢!
猜你喜欢
  • 2013-10-20
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 2015-12-01
相关资源
最近更新 更多