CakePHP-使用find()方法

CakePHP-使用find()方法,php,sql,cakephp,Php,Sql,Cakephp,我有一个艺术家模型和一个产品模型。他们的协会如下: Artist hasMany Product Product belongsTo Artist 艺术家可能没有任何产品。如何使用find()方法列出所有艺术家以及他们拥有的产品的数量?只要您的关系设置正确,这就是您所需要做的 $results = $ArtistModel->find('all'); 只要你的关系建立得当,这就是你所需要做的一切 $results = $ArtistModel->find('all'); 从ar

我有一个
艺术家
模型和一个
产品
模型。他们的协会如下:

Artist hasMany Product
Product belongsTo Artist

艺术家
可能没有任何
产品
。如何使用
find()
方法列出所有艺术家以及他们拥有的
产品的数量?

只要您的关系设置正确,这就是您所需要做的

$results = $ArtistModel->find('all');

只要你的关系建立得当,这就是你所需要做的一切

$results = $ArtistModel->find('all');

从artist controller,您可以执行以下操作:

$this->Artist->find('all')
要在视图中访问它,您需要使用set(),如下所示:

$this->set('artists', $this->Artist->find('all'));
$artists = $this->Artist->find('all');
array(
  [0] => array(
    [Artist] => array(
      'id' => 1,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(
      [0] => array(
        'id' => 1,
        ...
      ),
      [1] => array(
        'id' => 2,
        ...
      )

    )
  )
  [1] => array(
    [Artist] => array(
      'id' => 2,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(...)
  )
)

然后,您可以通过执行
print\r($artists)来查看数据在视图中。

从艺术家控制器可以执行以下操作:

$this->Artist->find('all')
要在视图中访问它,您需要使用set(),如下所示:

$this->set('artists', $this->Artist->find('all'));
$artists = $this->Artist->find('all');
array(
  [0] => array(
    [Artist] => array(
      'id' => 1,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(
      [0] => array(
        'id' => 1,
        ...
      ),
      [1] => array(
        'id' => 2,
        ...
      )

    )
  )
  [1] => array(
    [Artist] => array(
      'id' => 2,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(...)
  )
)
然后,您可以通过执行
print\r($artists)来查看数据在视图中。

您可以使用该功能在缓存中存储每个艺术家的产品数量

或者,如果您愿意,您可以(手动)使用ORM,它应该类似于:

$this->Artist->find('all',array('fields'=>array('Artist.*','COUNT(Product.id) as count_products')
                                'joins'=>array(array('table' => 'products',
                                                     'alias' => 'Product',
                                                     'type' => 'INNER',
                                                     'conditions' => array('Artist.id = Product.artist_id'))),
                                'group'=>'Artist.id'
));
希望这有帮助

您可以使用该功能在缓存中存储每个艺术家的产品数量

或者,如果您愿意,您可以(手动)使用ORM,它应该类似于:

$this->Artist->find('all',array('fields'=>array('Artist.*','COUNT(Product.id) as count_products')
                                'joins'=>array(array('table' => 'products',
                                                     'alias' => 'Product',
                                                     'type' => 'INNER',
                                                     'conditions' => array('Artist.id = Product.artist_id'))),
                                'group'=>'Artist.id'
));

希望这有帮助

您可以像这样使用一个简单的
查找
调用:

$this->set('artists', $this->Artist->find('all'));
$artists = $this->Artist->find('all');
array(
  [0] => array(
    [Artist] => array(
      'id' => 1,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(
      [0] => array(
        'id' => 1,
        ...
      ),
      [1] => array(
        'id' => 2,
        ...
      )

    )
  )
  [1] => array(
    [Artist] => array(
      'id' => 2,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(...)
  )
)
它返回和数组如下所示:

$this->set('artists', $this->Artist->find('all'));
$artists = $this->Artist->find('all');
array(
  [0] => array(
    [Artist] => array(
      'id' => 1,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(
      [0] => array(
        'id' => 1,
        ...
      ),
      [1] => array(
        'id' => 2,
        ...
      )

    )
  )
  [1] => array(
    [Artist] => array(
      'id' => 2,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(...)
  )
)
然后,您可以迭代结果并获得所需的信息:

foreach ( $artists as $artist ) {
  echo $artist['Artist']['name'];
  echo count($artist['Product']);
}

您可以像这样使用一个简单的
find
调用:

$this->set('artists', $this->Artist->find('all'));
$artists = $this->Artist->find('all');
array(
  [0] => array(
    [Artist] => array(
      'id' => 1,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(
      [0] => array(
        'id' => 1,
        ...
      ),
      [1] => array(
        'id' => 2,
        ...
      )

    )
  )
  [1] => array(
    [Artist] => array(
      'id' => 2,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(...)
  )
)
它返回和数组如下所示:

$this->set('artists', $this->Artist->find('all'));
$artists = $this->Artist->find('all');
array(
  [0] => array(
    [Artist] => array(
      'id' => 1,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(
      [0] => array(
        'id' => 1,
        ...
      ),
      [1] => array(
        'id' => 2,
        ...
      )

    )
  )
  [1] => array(
    [Artist] => array(
      'id' => 2,
      'other_field' => 'other_value',
      ...
    ),
    [Product] => array(...)
  )
)
然后,您可以迭代结果并获得所需的信息:

foreach ( $artists as $artist ) {
  echo $artist['Artist']['name'];
  echo count($artist['Product']);
}
你是指产品的“计数()”吗?你是指产品的“计数()”吗??