Welcome Guest, Not a member yet? Register   Sign In
DB Using CodeIgniter’s Model - $afterFind
#2

(06-18-2020, 11:47 AM)midav Wrote: Hi everyone, I’m doing my own project and I have a couple of tables and I want to make a connection between them through $afterFind, but in the documentation, it’s not clear how to do it correctly. I have a table of films and there are additional tables for films (genres, countries, actors) and I need to link them to the main table of films and set up specific output fields. Maybe someone already made a similar connection.

I also have a table of films, but I thought a different structure than yours. It was developed at CI3 and recently updated to CI4.
There are 3 tables: the one of films, of names - actors, directors, screenwriters - and one where I make the relationship between films and names.
It's a hobby for me, I'm not a professional, and it's just local, everything based on the IMDb website, including the layout, design, in my opinion the most difficult and complicated part.
When I started to develop I didn't know anything, so there are certainly a lot of structural errors that don't compromise the result.
The main table, "movies", includes all fields with information about each film except actors, the names that appear are highlights, and reference to awards received, restricted to Oscar, Golden Globe and BAFTA.

It is important to note that a migration was made from CI3 to CI4, the development was not specific to CI4.

Below, the "movies" controller with the "movie" and "awards" methods:
Code:
<?php namespace App\Controllers;
   
    // 20200508
   
    use CodeIgniter\Controller;
    use App\Models\MovieModel;
    use App\Models\AwardsModel;
    use App\Models\OscarModel;
    use App\Models\GlobeModel;
    use App\Models\BaftaModel;

    class Movies extends Controller {

    public function movie() {

        $model = new MovieModel();

        $data = [
            'result'    => $model->getMovie(),
            'stars'    => $model->getStars(),
            'runtime'  => $model->getRuntime(),
            'plustime'  => $model->getPlusTime(),
            'release'  => $model->getRelease(),
            'dir1'      => $model->getDir1(),           
            'dir2'      => $model->getDir2(),
            'dir3'      => $model->getDir3(),
            'dirx1'    => $model->getDirx1(),
            'dirx2'    => $model->getDirx2(),
            'dirx3'    => $model->getDirx3(),
            'wtr1'      => $model->getWtr1(),           
            'wtr2'      => $model->getWtr2(),
            'wtr3'      => $model->getWtr3(),
            'wtrx1'    => $model->getWtrx1(),
            'wtrx2'    => $model->getWtrx2(),
            'wtrx3'    => $model->getWtrx3(),
            'cast'      => $model->getCast(),
            'star1'    => $model->getStar1(),
            'star2'    => $model->getStar2(),
            'star3'    => $model->getStar3(),
        ];

        foreach ( $model->getMovie() as $row );

        $header = array (
            'icon' => 'favicon',
            'css' => 'movie',           
            'title' => $row->title.'&nbsp('.$row->ano.') | Créditos | wdeda',
            'action' => '/search/allmedia/',
            'placeholder' => 'Pesquisar'
        );

        echo view('templates/header', $header);
        echo view('content/movies/movie', $data);
        echo view('templates/footer');
    }

    public function awards() {

        $model1 = new MovieModel();
        $model2 = new AwardsModel();
        $model3 = new OscarModel;
        $model4 = new GlobeModel();
        $model5 = new BaftaModel;

        $data = [
            'award'    => $model2->getAwards(),
            'oscprz'    => $model3->oscprz(),
            'oscnom'    => $model3->oscnom(),
            'glbprz'    => $model4->glbprz(),
            'glbnom'    => $model4->glbnom(),
            'bftprz'    => $model5->bftprz(),
            'bftnom'    => $model5->bftnom()
        ];

        foreach ( $model1->getMovie() as $row);

        $header = array (
            'icon' => 'favicon',
            'css' => 'awards',
            'title' => $row->title.' | Premiações | wdeda',
            'action' => '/search/allmedia/',
            'placeholder' => 'Pesquisar'
        );

        echo view('templates/header', $header);
        echo view('content/movies/awards', $data);
        echo view('templates/footer');


    }
 

}


The model is extensive, 777 lines, so I'll just show the function where the associated names of the artists_movies table are taken:

Code:
public function getStars() {
        $request = \Config\Services::request();
        $uri = $request->uri;
        $id = $uri->getSegment(3);
        $db = db_connect();
        $sql = ("SELECT names.*, artists_movies.* FROM names, artists_movies
        WHERE artists_movies.movie_id=$id
        AND
        artists_movies.artist_id=names.id
        ORDER BY position, id ASC");
        $query = $db->query($sql);
        return $query->getResult();
    }

The homepage with prominence for films, there is also a catalog for music, records; the moviepage and awardspage:

Attached Files Thumbnail(s)
           
Reply


Messages In This Thread
RE: DB Using CodeIgniter’s Model - $afterFind - by wdeda - 06-18-2020, 06:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB