簡體   English   中英

Phalcon 3.0模型關系

[英]Phalcon 3.0 Model Relationships

我正在使用Phalcon 3.0.1構建一個很小的項目,只是為了學習如何在兩個表之間使用ORM模型關系,並且剛開始時遇到的第一個問題是關聯表中的數據聯接。 這是這些表的SQL代碼:

  CREATE TABLE `jobs` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `categories_id` int(11) unsigned NOT NULL,
  `location` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `how_to_apply` text NOT NULL,
  `author` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  KEY `fk_categories` (`categories_id`),
  CONSTRAINT `jobs_ibfk_1` FOREIGN KEY (`categories_id`) REFERENCES `categories` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='utf8_general_ci'


CREATE TABLE `categories` (
      `id` int(11) unsigned NOT NULL,
      `category_name` varchar(100) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='utf8_general_ci'

Jobs有一個類別的外鍵(categories_id)。 現在,嘗試在我的類別模型中初始化關系,例如:

public function initialize()
    {
        $this->hasManay('id', 'Jobs', 'categories_id', ['alias' => 'Jobs']);
    }

現在,當調用查找方法Categories::find() ,不會從Jobs表中返回相關數據。 我還注意到,我可以將任何愚蠢的東西放入hasMany()方法中,甚至不會捕獲異常。 看起來它被完全忽略了。 我什至可以做下面的事情,它不會崩潰。

public function initialize()
    {
        $this->hasManay('stupid', 'Doesnt_exist_table', 'more_stupid', ['alias' => 'Jobs']);
    }

我還要概述一下,我模型中的所有屬性都是公共的。 我已經遍歷了所有文檔,其他stackoverflow問題,github示例,並且我認為這應該可行。 任何幫助將不勝感激,因為我為此而生氣。

測試用例:

class Jobs extends Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->belongsTo('categories_id', 'Categories', 'id');
    }
}

class Categories extends Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->hasMany('id', 'Jobs', 'categories_id');
    }
}

class TestController extends Phalcon\Mvc\Controller
{
    public function testAction()
    {
        // get the first job
        $job = Jobs::findFirst();
        // get the (single) category that is related to this job
        $jobCategory = $job->getRelated('Categories');


        // get the first category
        $category = Categories::findFirst();
        // get all the jobs related to this category
        $jobsInThisCategory = $category->getRelated('Jobs');
    }
}

只有在對對象執行->getRelated()時,才會調用相關記錄。
因此,如果您將關系定義為

$this->hasMany('stupid', 'Doesnt_exist_table', 'more_stupid', ['alias' => 'Jobs']);

然后調用$category->getRelated('Jobs') ,將得到一個錯誤,因為該表Doesnt_exist_table不存在。

<?php
namespace App\Models;

use Phalcon\Mvc\Model;


class Categories extends Model
{

    // ....

    public function initialize()
    {
        $this->hasMany('id', __NAMESPACE__ . '\Articles', 'categories_id', array(
            'alias' => 'Categories',
            'reusable' => true
        ));
    }
}

use Phalcon\Mvc\Model;

class Articles extends Model
{

    // ....

    public function initialize()
   {
       $this->belongsTo("categories_id",  __NAMESPACE__ . "\Categories", "id", array(
          'alias' => 'Categories',
          'reusable' => true
        ));
   }

   public function getCategories($parameters = null)
   {
       return $this->getRelated('Categories', $parameters);
   }

}

在控制器中:

 public function indexAction()
{ 
    $articles = Articles::find([
      'order' => 'id DESC',
      'limit' => 2
    ]);
    $this->view->setVar('articles',$articles);
}

視場(伏):

{% for article in articles %}
  {{ article.title }}
  // ...
  {# Show Categories #}
  {{ article.categories.name }}
 {#  or #}
 {{ article.categories.id }}
 {#  or #}
 {{ article.categories.slug }}
{% endfor %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM