0

我正在使用 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']);
    }

现在调用 find 方法时Categories::find()不会从 Jobs 表中返回相关数据。我还注意到我可以在hasMany()方法中放入任何愚蠢的东西,它甚至不会捕获异常。看起来它完全被忽略了。我什至可以做类似下面的事情,它不会崩溃。

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

我还想概述一下我的模型中的所有属性都是公开的。我已经浏览了所有文档、其他 stackoverflow 问题、github 示例,我相信应该可以。任何帮助都将不胜感激,因为我对此很生气。

4

2 回答 2

0

测试用例:

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不存在。

于 2016-11-26T12:29:43.697 回答
0
<?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 %}
于 2016-12-05T16:39:52.173 回答