We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

model relations

Hello everyone! Iam trying to make seo system for my project, but i have troubles with that.

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

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

    }

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

    }

So problem is, when iam using Pages::findFirst, count($pages) always = 1, and when i change it to Pages::find, $pageSeo->page_title stop working.

  • i know, that this code can be better if use relations, but i cant make them working. Pls help me to deal with this problem!
edited Jun '15

findFisrt always return one record(if exists), so count($pages) = 1;

find return at least one record(if exists), may be you should try

$page = Pages::findFirst();
echo count($page->pageSeo)."\n";
foreach ($page->pageSeo as $pageSeo){
    echo $pageSeo->page_title."\n";
}

hope it helps



2.7k

findFisrt always return one record(if exists), so count($pages) = 1;

find return at least one record(if exists), may be you should try

$page = Pages::findFirst();
echo count($page->pageSeo)."\n";
foreach ($page->pageSeo as $pageSeo){
  echo $pageSeo->page_title."\n";
}

hope it helps

but it returns 1 even if there are no records for those conditions

edited Jun '15

but it returns 1 even if there are no records for those conditions

Are you sure there are no records? Try:

print_r(Pages::findFirst()->toArray())

to see what actually gets returned.



2.7k
edited Jun '15

everyone thanks, done that this way:

    $pages = Pages::findFirst(array(
        "page_controller = :controller: AND page_action = :action:",
        "bind" => array(
            "controller" => $controller,
            "action" => $action
        )
    ));

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

and changed hasMany to hasOne.