Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Database Access and ORM cakephp 3

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 317
    Comment on it

    The new version of cakephp comes with lots of new features and changes in it.

    Cakephp access the database with mainly two objects.

    First is table objects.

    Second is entities.

    Objects provide the access to the collection of data.

    Entities provide the access to the row record level data.

    They are responsible for the handling of any kind of data.

    ORM- Object-relational mapping, is a built in cakephp feature that works for relation database.

    It derives ideas from both ActiveRecord and Datamapper patterns.

    Just follow the cakephp conventions for the database and then you can start using ORM.

     

    use Cake\ORM\TableRegistry;
     $articles = TableRegistry::get('Articles');
     $query = $articles->find();
     foreach ($query as $row)
     { 
    echo $row->title;
     }

    This is to load data from table articles.

    This is very helpful as if you want to apply and association or any other type of method you can direct go to the model of that table and code there.

    namespace App\Model\Table;
    
    
    use Cake\ORM\Table;
     class ArticlesTable extends Table {
     }

    Now when you have concrete table classes you can create concrete entity class.

    They let you define accessor and mutator method.

    namespace App\Model\Entity;
    
    
    use Cake\ORM\Entity;
     class Article extends Entity {
     }

    Now you can load the enities

    use Cake\ORM\TableRegistry;
    
    
    
    // Now an instance of ArticlesTable.
     $articles = TableRegistry::get('Articles');
     $query = $articles->find();
     foreach ($query as $row) { 
    // Each row is now an instance of our Article class.
     echo $row->title; }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: