• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Relations Module

#11
@BasicApp Got it. I will look at a way to move functionality off of the constructor. I’m open to suggestions or a PR if you have ideas already.
Reply

#12
@BasicApp (et al) the latest release of Relations is out. A number of bug fixes and issues addressed, including that the model and entity traits no longer use constructors, which should make them easier to integrate.

I also have an example of the module in action, integrated into the CodeIgniter Playground, with browsable code and a server web version. See for more details: https://github.com/tattersoftware/codeig...s/issues/2
Reply

#13
Lightbulb 
Hi MGatner,

Thanks for your Tatter/Relations.
I am very excited while using it. But I am having a small problem and have not found a solution yet. Can you please suggest me how to use Tatter/Relations in this case.

I have a database table structure:
   

How to find all movie with genre, movie_cast, and movie.votes_avg
Best regards,
Reply

#14
@hungtrinhbds it looks like you have foreign keys defined properly, so Relations should pick up the relations you mentioned automatically. You should decide whether you want to implement this in models, entities, or both. Follow the docs to add the traits and then you can use “with(‘genre’)”. The only reason you might need to define your relations in a manual schema entry (see Tatter\Schemas) is because your table names are not plural.
Reply

#15
@MGatner: Thanks for your reply. My problem here is: How to use the query builder with Tatter/Relations in my case, with more than 2 relations. Your docs cover the relationship between 2 entities.

Sorry for my sentence!
Reply

#16
This my test code:
PHP Code:
$schemas service('schemas');
$schemas->draft()->get();
$movieModel = new \Hcv\Imdb\Models\Moviemodel;
$movieModel->where('votes_avg'7);
$movieModel->where('genre.genre_id''action');
$query $movieModel->get();
foreach (
$query->getResult() as $row) {
    echo 
$row->title;

This is Error:
PHP Code:
ErrorException
SQLite3
::query(): Unable to prepare statement1no such columngenre.genre_id 
Database connects OK.
PHP Code:
<?php namespace Hcv\Imdb\Models;

use \
Tatter\Relations\Traits\ModelTrait;

class 
Moviemodel extends \CodeIgniter\Model
{
    use 
ModelTrait;

    protected $table      'movie';
    protected $primaryKey 'movie_id';

    protected $returnType 'Hcv\Imdb\Entities\Movie';
    protected $useSoftDeletes false;

    protected $allowedFields = ['title''link_poster''storyline''rate''releasedate''month''year''budget''grossUSA''gross''genre_id'];

    protected $useTimestamps false;
    protected $createdField  '';
    protected $updatedField  '';
    protected $deletedField  '';

    protected $validationRules    = [
                    'movie_id'    => 'required|is_unique[movi.movie_id]',
            ];
    protected $validationMessages = [];
    protected $skipValidation     false;

    protected $with = ['genre''language''country''company''movie_cast''movie_crew'];

    public function hcvFind($value='')
    {
        # code...
    }

PHP Code:
<?php namespace Hcv\Imdb\Entities;

class 
Movie extends \CodeIgniter\Entity
{
        use \
Tatter\Relations\Traits\EntityTrait;  

        
protected $table      'movie';
        protected 
$primaryKey 'movie_id';
        
        public function 
setGenre_name($genre=[])
        {
                
$this->attributes['genre_name'] = 'genre_name';
                return 
$this;
        }


Reply

#17
You don’t use the query builder, you need to use the native Model find methods. In your case above you would do something like this:

foreach ($movieModel->findAll() as $movie)
{
echo $movie->title;
echo $movie->genre->genre_name;
}

If you would like to see an example in use, check out the CodeIgniter Playground Branch I modified to use Relations:

https://github.com/codeigniter4projects/...:relations
Reply

#18
(08-11-2020, 04:26 AM)MGatner Wrote: You don’t use the query builder, you need to use the native Model find methods. In your case above you would do something like this:

foreach ($movieModel->findAll() as $movie)
{
echo $movie->title;
echo $movie->genre->genre_name;
}

If you would like to see an example in use, check out the CodeIgniter Playground Branch I modified to use Relations:

https://github.com/codeigniter4projects/...:relations

@MGatner,

Can you suggest to me in the case:
when findAll() returns 1000 results but only one of them matches genre_name I need.
Reply

#19
The point of Relations is that it handles the direct ID queries for you. It should only load the one genre that you need.
Reply

#20
(08-14-2020, 02:38 PM)MGatner Wrote: The point of Relations is that it handles the direct ID queries for you. It should only load the one genre that you need.

Thanks for your Tatter/Relations!
I have almost used it very effectively for my database!
But if there is a way to work with Query Builder I haven't figured it out yet. Can you suggest to me anyway!

Best regards,
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.