简体   繁体   English

CakePHP从类所属的表中获取数据

[英]CakePHP get data from a table that class belongs_to

I have a model in cakePHP: 我在cakePHP中有一个模型:

class PagesTable extends Table
{
  public function initialize(array $config)
{
    $this->addBehavior('Timestamp');

    $this->belongsTo('Pagecats');

    $this->belongsToMany('Users');
}

and another model PagecatsTable: 和另一个模型PagecatsTable:

class PagecatsTable extends Table
{
public function initialize(array $config)
{
    $this->addBehavior('Timestamp');
}

which its table has two coloumns id and category 它的表有两个列idcategory

How can I get the category in a function in my PagesController? 如何在我的PagesController的函数中获取category

In your PagesController, do you want the pages and their associated records? 在您的PagesController中,您是否需要页面及其相关记录? If so, you could try the below mentioned code: 如果是这样,您可以尝试下面提到的代码:

// PagesController.php

$data = $this->Pages->find()
  ->contain('Pagecats')
  ->all();

pr($data->toArray()); // Check if you're getting the desired result.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM