簡體   English   中英

CakePHP從類所屬的表中獲取數據

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

我在cakePHP中有一個模型:

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

    $this->belongsTo('Pagecats');

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

和另一個模型PagecatsTable:

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

它的表有兩個列idcategory

如何在我的PagesController的函數中獲取category

在您的PagesController中,您是否需要頁面及其相關記錄? 如果是這樣,您可以嘗試下面提到的代碼:

// 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