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.

help facing of relationships

Hello, i need help facing of relationships thrugh models, in particular need to understand how best to manage to be able to take advantage of these.

I have this table:

CREATE TABLE `album` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(50) NULL DEFAULT NULL,
    `created_at` DATE NULL DEFAULT NULL,
    `updated_at` DATETIME NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)

CREATE TABLE `images` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NOT NULL,
    `tags` VARCHAR(50) NOT NULL,
    `created_at` DATE NULL DEFAULT NULL,
    `updated_at` DATETIME NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)

CREATE TABLE `user` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `email` VARCHAR(50) NOT NULL DEFAULT '0',
    `password` VARCHAR(128) NOT NULL DEFAULT '0',
    `role` ENUM('guest','user','admin') NOT NULL DEFAULT 'user',
    `created_at` DATETIME NULL DEFAULT '0000-00-00 00:00:00',
    `updated_at` DATETIME NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (`id`),
    UNIQUE INDEX `email` (`email`)
)

CREATE TABLE `album_link` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `album_id` INT(10) NOT NULL,
    `image_id` INT(10) NOT NULL,
    `user_id` INT(10) NOT NULL,
    `created_at` DATE NULL DEFAULT NULL,
    `updated_at` DATETIME NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)

How i can manage relation betwin these tables, using models?

Thank you in advance.

you should take the time and read the manual first! https://docs.phalconphp.com/en/latest/reference/models.html

Readed yet! im asking the best method to do that! hasMany or many.to-many...

edited Jan '16

Album -> hasMany images / hasOne link (not sure what this link do, maybe is wrong has one, couse of image_id, but in that case, must be AlbumLinkS)

Images -> belongsTo Album / hasOne link (its not bad idea to rename it to album_images... just a thought)

User -> hasMany albums / hasOne link

AlbumLink -> belongsTo Album, Image... etc

I didnt need many-to-many relation... not in that case. Its up to u :) Good luck

just time to try it ;)

OK...im stuck again, How i can take advantage from this models?

<?php $album->Images->name; ? ?>

edited Jan '16

Thats easy. This relation will be hasMany, so, u can use something like that:

  $album = Models\Album::findFirst();
  $images = $album->getImages(); // Or just $album->Images... depends of alias or your needs and configuration
  foreach($images as $img) {
      echo $img->title;
  }

If u have hasOne/belongsTo relation (emagine that album has only one image), u can use it the same way u describe : $album->Image->name;

Thank you, but findFirst return only 1 row into AlbumLink table, if i try

    $album = AlbumLink::find("album_id = $album_id");
     $images = $album->getImages();

     foreach($images as $img) {
         echo $img . "<br/>";
     }

return: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::getImages()

edited Jan '16
$album = AlbumLink::findFirstByAlbumId($album_id);
$images = $album->getImages();
foreach ($images as $img) {
      echo $img . "<br/>";
}


1.4k
Accepted
answer
edited Jan '16

fetch only one record but, i find a way to do this, i dont know if is the "best" but work:

     $album_id = $this->request->getPost('album_id');

     $conditions = "album_id = :album_id:";
     $parameters = array(
         "album_id" => "$album_id"
     );

     $album = AlbumLink::find(
         array(
              $conditions,
              "bind" => $parameters
             )
     );


     $this->view->setVar('album', $album);


     -----VOLT TEMPLATE-----

     {% for img in album %}
     ..
     ....
     <img id="{{ img.Images.id }}" class="img-responsive" src="../albums_images/{{ img.Images.name }}" alt="">
     ...
     ..
     {% endfor %}

:) seems fine, but u can consider that tips:

  1. too much variables, more simple/faster is just:
  $this->view->album = AlbumLink::find([
    'conditions' => 'album_id = {album:int}',
    'bind' => ['album' => (int)$this->request->getPost('album_id')]
  ]);
  1. Try to think about relations and their names depends of number. For example, that {{ img.Images.id }} its better to be just {{ img.Image.id }}

  2. Check for exceptions - u can check in controller (for example) if u find any AlbumLink. If u didnt - return 404 error:

$album = AlbumLink::find([
  'conditions' => 'album_id = {album:int}',
  'bind' => ['album' => (int)$this->request->getPost('album_id')]
]);

if (!$album) {
  $this->response->setStatusCode(404);  // switch to page 404 or redirect to it. (show diferent view)

  return ...
}

$this->view->album = $album;

Good luck.