Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

MySQL two columns relate to another tables column

Hey community!

I have one table "Tickers" with the columns "teamhome" and "teamguest" and another table "Teams" with "id" and "name". Now I want to get the teams without separately calling the teams-model.

So I think the code should look like the following

foreach (Tickers::findFirst() as $ticker) {
    $teamHome = $ticker->teamHomeObj;
    $teamGuest = $ticker->teamGuestObj;

And NOT:

foreach (Tickers::findFirst() as $ticker) {
    $teamHome = Teams::findFirst("id=".$ticker->team_home);
    $teamGuest = Teams::findFirst("id=".$ticker->team_guest);

greetings Eike


First: Thanks for your reply! I already read this part. In my understanding there is only mentioned the relation of one column of a table to one column of another table. But I have TWO COLUMNS each of them relating to ONE COLUMN of another table Am I wrong?

Edit: Maybe it has something to do with this sentence "You could also use arrays to define multiple fields in the relationship." But there is no example in the docs :(

edited Oct '14

I guess you should use OneToMany relation. One Ticker has Many (2) team. And use:

foreach (Tickers::find() as $ticker) {
foreach($ticker->getTeam() as $team){
      //do some stuff with your team
$teamHome = Teams::findFirst("id=".$ticker->team_home);
$teamGuest = Teams::findFirst("id=".$ticker->team_guest);

Hope this help P.S. findFirst() returns 1 row, find() returns N rows

edited Oct '14

I cannot find the doku for the method oneToMany. Do you mean hasMany? That is what I already have.


$this->hasMany("team_home", "Teams", "id");
$this->hasMany("team_guest", "Teams", "id");


$this->belongsTo("id", "tickers", "team_home");
$this->belongsTo("id", "tickers", "team_guest");
foreach (Tickers::find() as $ticker) {
    foreach($ticker->getTeam() as $team) {
        // Here I only get the Team of the second column

Edit: Even in the mysql log only one team is selected

  4 [Wed, 16 Oct 13 19:41:35 +0200][INFO] SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='tickers'
  5 [Wed, 16 Oct 13 19:41:35 +0200][INFO] DESCRIBE `tickers`
  6 [Wed, 16 Oct 13 19:41:35 +0200][INFO] SELECT `tickers`.`team_home`, `tickers`.`team_guest` FROM `tickers` WHERE `tickers`.`id` = 20
  7 [Wed, 16 Oct 13 19:41:35 +0200][INFO] SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='teams'
  8 [Wed, 16 Oct 13 19:41:35 +0200][INFO] DESCRIBE `teams`
  9 [Wed, 16 Oct 13 19:41:35 +0200][INFO] SELECT `teams`.`id`, `teams`.`name` FROM `teams` WHERE `teams`.`id` = :0


Seems alias should solve this problem.

        $this->hasMany('id', 'Fresh\Models\UserChangePassword', 'usersId', array(
            'alias' => 'passwordChanges',
            'foreignKey' => array(
                'message' => 'User cannot be deleted because he/she has activity in the system'



edited Oct '14

Thanks a lot @xAockd and @cuhuak! The alias was the solution.

My code now: Teams

$this->belongsTo("id", "Tickers", "team_home");
$this->belongsTo("id", "Tickers", "team_guest");


$this->hasMany("team_home", "Teams", "id", array(
    "alias" => "teamHomeObjekt"
$this->hasMany("team_guest", "Teams", "id", array(
    "alias"  => "teamGuestObjekt"