Tuesday, March 31, 2015

Re: CakePHP 2 v CakePHP 3 SQL

Hopefully from our previous messages you can guess it yourself:

$options = array(
                
'conditions' => array(
                    
'Rentals.id' => $id,

                    
),
                    
'contain' => array(
                        
'Trailers' => array(

                            
'fields' => array('id', 'number', 'make', 'year', 'license', 'vin', 'skirt')
                        
),
                        
'Lessees' => array(
                            
'fields' => array('id', 'account', 'name', 'address_1', 'address_2', 'city', 'state', 'zip_1', 'zip_2')
                        
)
                    
)
                
);


            $rental
 = $this->Rentals->find('all', $options)->first();

You can also use the where(), select() and contain() methods if you prefer not using an array.

On Monday, March 30, 2015 at 10:29:40 PM UTC+2, Dave Edwards wrote:
BUMP!

On Tuesday, 24 March 2015 20:33:49 UTC, Dave Edwards wrote:
Thanks for the reply,

That being the case, I am pleasantly surprised that despite the documentation making a big play on how different the new ORM is, I can (if I choose) create my queries in much the same way.

The example I posted was a effectively a find 'all' (paginated using a limit clause). What about a find first, is this still as straightforward? Here is an example from the same controller, how would that translate please?


            $options = array(
               
'conditions' => array(
                   
'Rental.id' => $id,
                   
),
                   
'contain' => array(
                       
'Trailer' => array(
                           
'fields' => array('id', 'number', 'make', 'year', 'license', 'vin', 'skirt')
                       
),
                       
'Lessee' => array(
                           
'fields' => array('id', 'account', 'name', 'address_1', 'address_2', 'city', 'state', 'zip_1', 'zip_2')
                       
)
                   
)
               
);


            $this
->request->data = $this->Rental->find('first', $options);

Dave

On Tuesday, 24 March 2015 13:04:21 UTC, 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: