简体   繁体   English

Phalcon 3.0模型关系

[英]Phalcon 3.0 Model Relationships

I'm building a tiny project using Phalcon 3.0.1 just to learn how to use ORM model relationships between two tables and just on the start encountered first problem with joining data from related tables. 我正在使用Phalcon 3.0.1构建一个很小的项目,只是为了学习如何在两个表之间使用ORM模型关系,并且刚开始时遇到的第一个问题是关联表中的数据联接。 Here's my SQL code for those tables: 这是这些表的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 has a foreign key for categories (categories_id). Jobs有一个类别的外键(categories_id)。 Now, trying to init relationship in my categories model like: 现在,尝试在我的类别模型中初始化关系,例如:

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

Now when calling find method Categories::find() is not returning related data from Jobs table. 现在,当调用查找方法Categories::find() ,不会从Jobs表中返回相关数据。 I've also notice that I can put anything stupid inside hasMany() method and it even won't catch exception. 我还注意到,我可以将任何愚蠢的东西放入hasMany()方法中,甚至不会捕获异常。 It looks like it is completely ignored. 看起来它被完全忽略了。 I can even do something like below and it won't crash. 我什至可以做下面的事情,它不会崩溃。

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

I would like to also outline that all properties in my models are public. 我还要概述一下,我模型中的所有属性都是公共的。 I've gone trough all the docs, other stackoverflow questions, github examples and I believe that should work. 我已经遍历了所有文档,其他stackoverflow问题,github示例,并且我认为这应该可行。 Any help will be appreciated as I'm going mad about it. 任何帮助将不胜感激,因为我为此而生气。

Test case: 测试用例:

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');
    }
}

The related records will only be called when you do ->getRelated() on your object. 只有在对对象执行->getRelated()时,才会调用相关记录。
So if you defined your relation as 因此,如果您将关系定义为

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

And you call $category->getRelated('Jobs') , you will get an error because that table Doesnt_exist_table doesn't exist. 然后调用$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);
   }

}

In Controller: 在控制器中:

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

In View(Volt): 视场(伏):

{% 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