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

Get info after create relationship between tables

I'm just a newbie and finding out PhalconPHP.

In one example, I have three tables with MyISAM engine:

In /model/Teams.php


public function initialize()
{
    $this->hasMany("teamID","TeamsLeagues","teamID");
}

In /model/Leagues.php


public function initialize()
{
    $this->hasMany("leagueID","TeamsLeagues","leagueID");
}

In /model/TeamsLeagues.php


public function initialize()
{
    $this->belongsTo("teamID","Teams","teamID");
    $this->belongsTo("leagueID","Leagues","leagueID");
}

I want to show more the column League when display the Teams, how can I do that?

I try this but not succeed:

In /controllers/TeamsController.php


    public function searchAction()
    {

        $numberPage = 1;
        if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, "Teams", $_POST);
            $this->persistent->parameters = $query->getParams();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $parameters = $this->persistent->parameters;
        if (!is_array($parameters)) {
            $parameters = array();
        }
        $parameters["order"] = "teamID";

        $teams = Teams::find($parameters);
        if (count($teams) == 0) {
            $this->flash->notice("The search did not find any teams");

            return $this->dispatcher->forward(array(
                "controller" => "teams",
                "action" => "index"
            ));
        }

        $paginator = new Paginator(array(
            "data" => $teams,
            "limit"=> 25,
            "page" => $numberPage
        ));

        $this->view->page = $paginator->getPaginate();
    }

In /view/teams/search.phtml


        <tr>
            <td><?php echo $team->teamID ?></td>
            <td><?php echo $team->name ?></td>

            <td><?php echo $team->teamsLeagues->name ?></td>

            <td><?php echo $team->shortName ?></td>
            <td><?php echo $team->logo ?></td>
            <td><?php echo $team->description ?></td>
            <td><?php echo $this->tag->linkTo(array("teams/edit/" . $team->teamID, "Edit")); ?></td>
            <td><?php echo $this->tag->linkTo(array("teams/delete/" . $team->teamID, "Delete")); ?></td>
        </tr>

It show notice: Notice: Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$name in E:\xampp\htdocs\football\app\views\teams\search.phtml on line 32



6.6k

You can use a many-to-many relation here.

$this->hasManyToMany(
            "teamID",
            "TeamsLeagues",
            "teamID", "leagueID",
            "Leagues",
            "leagueID",
            array(
                "alias" => "teamsLeagues"
            )
        );

I think this should work and you can access your property as shown in your example.



6.0k

I tried to add that relationship into Teams models but nothing else happen.

Beside, do i have to use tables with InnoDDB engine (I just want to use MyISAM engine)?

You can use a many-to-many relation here.

$this->hasManyToMany(
           "teamID",
           "TeamsLeagues",
           "teamID", "leagueID",
           "Leagues",
           "leagueID",
          array(
              "alias" => "teamsLeagues"
          )
       );

I think this should work and you can access your property as shown in your example.



6.6k

Did you declare all the attributes (inclusive primary, foreign keys) in your models?

InnoDB is faster and it supports transactions. It's recommended to use InnoDB in new projects ;)



6.0k

I refered the website INVO by Phalcon but do not see they create the foreign keys in db, when I change the InnoDB engine to MyISAM, website stil runs normally.

As I know, InnoDB is not faster than MyISAM when you just need selected records.