【问题标题】:Phalcon 3.0 Model RelationshipsPhalcon 3.0 模型关系
【发布时间】:2016-11-26 10:19:53
【问题描述】:

我正在使用 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 示例,我相信应该可以。任何帮助都将不胜感激,因为我对此感到很生气。

【问题讨论】:

  • hasMany (hasManay) 方法中有一个错字,但这是在发布问题时发挥作用的,对代码本身没有任何影响。

标签: php phalcon phalcon-orm


【解决方案1】:

测试用例:

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

【讨论】:

    【解决方案2】:
    <?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 %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2018-01-14
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      相关资源
      最近更新 更多