CakePHP查找删除字段或递归查找

CakePHP查找删除字段或递归查找,cakephp,find,field,Cakephp,Find,Field,我正试图通过指定要使用的字段来精简我的finds查询。我不明白如何为我的HABTM字段指定特定字段 这是我的发现: $lastviewed = $this->Lastviewedproduct->find('all', array( 'fields' => array( 'Lastviewedproduct.id', 'Lastviewedproduct.sessionid', 'Lastviewedproduct.product_id',

我正试图通过指定要使用的字段来精简我的finds查询。我不明白如何为我的HABTM字段指定特定字段

这是我的发现:

$lastviewed = $this->Lastviewedproduct->find('all', array(
    'fields' => array(
        'Lastviewedproduct.id', 'Lastviewedproduct.sessionid', 'Lastviewedproduct.product_id',
        'Product.id', 'Product.name', 'Product.slug', 'Product.price',
        //'Category.id',
        //'Mediafile.*',
    ),
    'conditions' => array(
        'Lastviewedproduct.status' => 'active',
        'Lastviewedproduct.sessionid' => $this->Session->read('Companyname.sessionid'),
        'Product.hidden_on_site' => 0,
        'Product.visibility' => 1,
        'Product.status' => 'active',
    ),
    'order' => 'Lastviewedproduct.modified DESC',
    'limit' => 5,
    'recursive' => 2,
));
这就是它的回报:

(int) 2 => array(
    'Lastviewedproduct' => array(
        'id' => '97',
        'sessionid' => '14035312318244955',
        'product_id' => '2'
    ),
    'Product' => array(
        'id' => '2',
        'name' => 'blokhut 2',
        'slug' => 'blokhut-2',
        'price' => '200.00',
        'Category' => array(
            (int) 0 => array(
                'id' => '218',
                'zosomodule_id' => '25',
                'user_add_id' => '0',
                'user_update_id' => '0',
                'created' => '2013-11-27 10:00:00',
                'modified' => '2013-11-27 10:00:00',
                'status' => 'active',
                'visibility' => true,
                'parent_id' => '2',
                'shipment_id' => '1',
                'name' => 'Metalen Tuinbergingen ',
                'slug' => 'metalen-tuinbergingen-',
                'pagetitle' => 'Metalen Tuinbergingen ',
                'content' => '<p class="p1"><span class="s1">Metalen tuinbergingen zijn een praktische en kwalitatieve oplossing voor meer bergruimte in uw tuin. Wij hebben diverse merken metalen bergingen zoals </span><span class="s2"><a href="/biohort-avantgarde/">Biohort</a></span><span class="s1">&nbsp;en </span><span class="s2"><a href="/yardmaster-metalen-bergingen/">Yardmaster</a></span><span class="s1">. Hier hebben wij tevens ook tuinkasten en opbergboxen van.</span></p>
<p class="p1"><span class="s1"><br /></span></p>
<p class="p1"><span class="s1">Mocht u op zoek zijn naar andere varianten tuinbergingen, bekijk dan ons gehele assortiment </span><span class="s2"><a href="/tuinbergingen/">tuinbergingen</a>.</span></p>',
                'metatitle' => 'Metalen tuinbergingen',
                'metadescription' => null,
                'discountoffer' => false,
                'discount_normal' => '0.00',
                'discount_max' => '0.00',
                'discount_ideal' => '0.00',
                'cost_rembours' => '0.00',
                'in_mainmenu' => true,
                'hide_sidebar' => false,
                'show_block' => true,
                'sort_order' => '0',
                'ProductsCategory' => array(
                    'id' => '2',
                    'created' => '2014-01-01 10:00:00',
                    'modified' => '2014-01-01 10:00:00',
                    'product_id' => '2',
                    'category_id' => '218'
                )
            )
        ),
        'Mediafile' => array()
    )
),
(int)2=>数组(
“Lastviewedproduct”=>数组(
'id'=>'97',
'sessionid'=>'14035312318244955',
“产品标识”=>“2”
),
'Product'=>数组(
'id'=>'2',
'name'=>'blokhut 2',
“slug”=>“blokhut-2”,
“价格”=>“200.00”,
“类别”=>数组(
(int)0=>数组(
'id'=>'218',
“zosomodule_id”=>“25”,
“用户添加id”=>“0”,
'用户\u更新\u id'=>'0',
“已创建”=>“2013-11-27 10:00:00”,
“已修改”=>“2013-11-27 10:00:00”,
'状态'=>'活动',
“可见性”=>正确,
“父项id”=>“2”,
“装运id”=>“1”,
'name'=>'Metalen Tuinbergingen',
“slug”=>“metalen tuinbergingen-”,
“pagetitle”=>“Metalen Tuinbergingen”,
“内容”=>”

在威斯康星州,人们可以通过各种不同的金属材料,通过使用不同的包装箱,在公共交通工具上进行实践


Mocht u op zoek zijn naar Ander varianten tuinbergingen、bekijk和ons gehele产品组合。

', “metatitle”=>“Metalen tuinbergingen”, “metadescription”=>null, “折扣供应商”=>错误, “折扣正常值”=>“0.00”, “折扣最大值”=>“0.00”, “理想折扣”=>“0.00”, 'cost_rembours'=>'0.00', “在主菜单中”=>true, “隐藏侧边栏”=>错误, 'show_block'=>true, “排序顺序”=>“0”, “ProductsCategory”=>数组( 'id'=>'2', “已创建”=>“2014-01-01 10:00:00”, “修改”=>“2014-01-01 10:00:00”, “产品标识”=>“2”, “类别识别码”=>“218” ) ) ), 'Mediafile'=>array() ) ),
我不想要所有这些类别字段,但只想要
Category.id
Category.name
Category.slug
。在我的发现中我该怎么说?如您所见,我尝试在我的字段数组中添加
Category.id
,但随后出现错误。我读了一些关于containable的文章,但是我没有做到这一点,我不知道这是否是解决方案


提前感谢您的帮助

添加字段参数,并在其中提供与类别模型的关系

public $hasMany = array(
        'Category' => array(
            'className' => 'Category',
            'fields' => array('Category.id','Category.name','Category.slug')
        )
    );
您可以使用来完成此任务。你应该这样写:
1附加行为

  • 永久的

    class Lastviewedproduct extends AppModel {
        public $actsAs = array('Containable');    
        ....
    }
    
  • 随需应变

    $this->Lastviewedproduct->Behaviors->load('Containable');
    
2指定字段

$this->Lastviewedproduct->find('all', array(
    'contain' => array(
        'Product' => array(
            'Category' => array(
                'fields' => array('id','name','slug')               
            ),            
        ),
        'Mediafile'
    )
));
正如迪米特里在上文中所说的那样,这是一种方法:

在您的Models/AppModel.php(如果它不存在,则创建它)中,更改调用以具有以下内容:

class AppModel extends Model {
    public $actsAs = array('Containable');
}
然后,您将按照上面Dimitry所说的操作—这将允许您只返回所需的字段

'recursive' => 2 

最终会导致您的应用程序运行缓慢,使用起来会很痛苦。大多数人在AppModel中设置recursive=1,并在其余时间使用containable。

但随后我会丢失整个类别和Mediafile数组。我想保留他们,因为我需要他们的信息。我只是不需要他们所有的领域。所以我从我的发现中得到了我需要的所有信息,但是我想缩小我的发现,这样我就只能得到必要的字段。我想我在问题中解释得很好。你是说我的模型在Model.php中是如何绑定的?它们都是HasAndBelongTo关系。是的,我想查看关系LastViewedProduct有一个BelongTo产品。该产品与类别和Mediafile具有相同的属性。它们只是CakePHP生成的关系。你们有并没有给出动态关系或模型中的关系?这很有效,谢谢!我必须在模型中添加线条,正如Voycey提到的那样,这样才能工作。也许你可以更新你的答案使之完整?