Tuesday, March 24, 2015

Re: CakePHP 2 v CakePHP 3 SQL

This is probably going to be a surprise for you. This is the equivalent code in CakePHP 3

public function archived($id = null) {


        $options 
= array(
          
'limit' => 10,
          
'order' => ['Trailers.number' => 'ASC'],
          
'fields' => array('id', 'trailer_id', 'lessee_id', 'date_out', 'rented', 'agreement_number'),
          
'conditions' => array(
              
'Rentals.deleted' => true,
          
),
          
'contain' => array(
              
'Trailers' => array(
                  
'fields' => array('id', 'number', 'make', 'license'),
              
),
              
'Lessees' => array(
                  
'fields' => array('id', 'account', 'name'),
              
)
          
)
        
);


        $this
->paginate = $options;
        $this
->set('rentals', $this->paginate());
 
}


Perhaps you cannot spot the differences, but mainly the only one is that model names (now called Tables) they are in plural. That it!

All the find interface from 2.x was kept, except for recursive. If you want to use a more expressive way of building queries, then go to the manual and figure out about all the new options you have now.

On Tuesday, March 24, 2015 at 2:04:21 PM UTC+1, Dave Edwards wrote:
Here is a sample of some code I use in a Trailer rental application. This is a method from the Rental Controller.

Rental hasMany Trailers and hasMany Lessees

public function archived($id = null) {


        $options
= array(
         
'limit' => 10,
         
'order' => 'Trailer.number ASC',
         
'fields' => array('id', 'trailer_id', 'lessee_id', 'date_out', 'rented', 'agreement_number'),
         
'conditions' => array(
             
'Rental.deleted' => '1',
         
),
         
'contain' => array(
             
'Trailer' => array(
                 
'fields' => array('id', 'number', 'make', 'license'),
             
),
             
'Lessee' => array(
                 
'fields' => array('id', 'account', 'name'),
             
)
         
)
       
);


        $this
->paginate = $options;
        $this
->set('rentals', $this->paginate());
 
}


Turning off recursive, and using Containable provides a very flexible and readable method of building a query. Adding conditions, fields, associated models etc is a snip. Whoever produced this method of working should be fully credited.

My question is, how would something like this be created in CakePHP 3? There seem to be many many more pages to read and understand regarding the new ORM, and I'm really unsure how simple it will be to convert. Will I be able to achieve it in the a similar manner, and will it be as readable, and easy to edit?

Are there any examples in the new Manual (I can't find any), where the new ORM is used to bring all the new methods of working together in much the same way as the method above?

Thanks

Dave

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: