Answer the question
In order to leave comments, you need to log in
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');
}
}
}
<?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'
));
}
}
<?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'
));
}
}
$robot = Robots::findFirst();
$robotsParts = $robot->robotsParts; // all the related records in RobotsParts
$pages = Pages::findFirst(array(
"page_controller = :controller: AND page_action = :action:",
"bind" => array(
"controller" => $controller,
"action" => $action
)
));
$pages->pageSeo->page_title; //для отображения тайтла страницы
Answer the question
In order to leave comments, you need to log in
response.get('updates') doesn't throw an exception
updates = response.get('updates')
if not updates:
data = auth(token)
Pages::find() - returns an array of posts
Pages::findFirst() - returns a single record
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question