F
F
fedot13252015-06-26 17:03:13
Python
fedot1325, 2015-06-26 17:03:13

How to properly set up relationships between models in phalcon?

Hello.
Only recently I started to master phalcon, and I ran into some problems when I decided to fasten the database.
I looked around a bunch of topics, but I still couldn’t set up relationships between models in my case.
I want to pull the title from the database, etc., I tried to implement it like this:
ControllerBase:

<?php
namespace Coursor\Controllers;

use Coursor\Models\Pages;
use Coursor\Models\PageSeo;

class ControllerBase extends \Phalcon\Mvc\Controller {

    protected function checkAjaxRequired() {
        if (!$this->request->isAjax()) {
            $this->response->setStatusCode(404, "Not Found");

            $this->dispatcher->forward(array(
        'namespace' => 'Coursor\Controllers',
        'controller' => 'errors',
        'action' => 'error404'
            ));
            return false;
        }
        return true;
    }

    public function initialize() {

    $action = $this->router->getActionName();
    $controller = $this->router->getControllerName();

        $pages = Pages::findFirst(array(
      "page_controller = :controller: AND page_action = :action:",
      "bind" => array(
        "controller" => $controller,
        "action" => $action
      )
    ));
    
    $pageSeo = PageSeo::findFirstByPage_id($pages->id);

        if (count($pages) != 0) {
      $this->tag->setTitle($pageSeo->page_title . ' ' . count($pages));
        }
    else{
            $this->tag->setTitle('NO TITLE');
    }
    }
}

page model:
<?php
namespace Coursor\Models;

use Phalcon\Mvc\Model;

class Pages extends Model {

    public $id;

    public $page_controller;
  
    public $page_action;
  
    public function initialize() {
    $this->setSource("pages");
        $this->hasMany("id", "Coursor\Models\PageSeo", "page_id", array(
            'alias' => 'pageSeo'
        ));
    }

}

Page Seo model:
<?php
namespace Coursor\Models;

use Phalcon\Mvc\Model;

class PageSeo extends Model {

    public $id;

    public $page_id;

    public $page_title;
  
  public $meta_description;
   
    public $meta_keywords;

    public function initialize() {
    $this->setSource("page_seo");
        $this->belongsTo("page_id", "Coursor\Models\Pages", "id", array(
      'alias' => 'page'
    ));
    }

}

It seems that this method works, but not everything is smooth, when using Pages::findFirst, count($pages) is always equal to 1, even if there is no record in the database, but when using Pages::find, count($pages) works as it should, but $pages->id stops working.
Tell me how to do it right?
UPD
Thank you, I solved the problem with displaying the absence of a record in the database. The question remains about the correct use of relations as in the example in the docs:
$robot = Robots::findFirst();
$robotsParts = $robot->robotsParts; // all the related records in RobotsParts

only
$pages = Pages::findFirst(array(
      "page_controller = :controller: AND page_action = :action:",
      "bind" => array(
        "controller" => $controller,
        "action" => $action
      )
    ));
    
    $pages->pageSeo->page_title; //для отображения тайтла страницы

this cannot be done.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Statkevich, 2018-10-05
@timur102

response.get('updates') doesn't throw an exception

updates = response.get('updates')
if not updates:
    data = auth(token)

S
Sergey Semenko, 2015-06-26
@fedot1325

Pages::find() - returns an array of posts
Pages::findFirst() - returns a single record

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question