cakephp_CakePart3.pdf

(43 KB) Pobierz
Microsoft Word - CakePHP Part 3.doc
CakePHP Part 3 – Goodbye to Scaffolding
Welcome back! Things are progressing quite nicely with ekklesia, our church youth
group event manager. Last time, we were using scaffolding for our views, we got our
validation rules under control, and we created relationships between our tables. We were
left with this question regarding our relationships, though: why is it that, when viewing
an event or user, I can see associated registrations, but instead of having event names and
user names, I’m getting id numbers?
Well, it all has to do with how we set up our relationships, and the fact that they are being
called from the user and event controllers. If you view them from the registrations
controller, everything works. Never fear, there is a way to fix this, but it means we have
to give up our scaffolding. Scaffolding is great, since it gives you all this functionality,
but it is all done automagically, so you can’t edit the code. Luckily, bake will fix this for
us.
Baking New Controllers
Luckily, we haven’t modified our controller code at all, so re-baking it won’t hurt us a
bit. Open a command prompt and go to c:\web\projects\cake\cake\scripts. At the
prompt, type: php bake.php –app c:\web\projects\ekklesia.
Let’s bake a new controller. We’ll start with events. Let’s bake interactively. We won’t
use other models, helpers, or components. We will use sessions. This time, we are going
to include some basic class methods. This will take scaffolding and actually write it out
for us. We’re not going to do admin routing.
Now bake your other controllers.
OK, we’re halfway done. If you try to view your app now, you’ll get an error about a
missing view.
Baking the View
Views contain your presentation logic, JavaScript, css, html, etc. When we were using
scaffolding, the views were created automagically. Now, we have to create them. A
view is really just an html snippet. It can have html, and will usually have php code as
well. Luckily, bake will make our views for us. Run bake again, and this time choose
view. Let’s build the events view first. We will do this interactively. We want to build
some scaffolded views, which will give us most of the advantages of scaffolding, but
we’ll be able to customize the view. We don’t want to do admin routing. Now bake your
other views.
When you view your app now, it should look very similar to your scaffolded view from
last time. It may appear a little different, but that’s OK. We’re going to change it even
more.
Editing the View
Now, we want to fix our user view so that the associated registrations will show us
names, not ids. This is a 2 step process. First, we will edit the controller, then we will
edit the view.
Open the users_controller.php file in your favorite editor. It’s in the controllers folder.
You will see several functions. Find this one:
function view( $id = null) {
if (! $id ) {
$this ->Session->setFlash( 'Invalid id for User.' );
$this ->redirect( '/users/index' );
}
$this ->set( 'user' , $this ->User->read(null, $id ));
}
Add this line before the if:
$this ->User->recursive = 2 ;
This tells Cake to dig a little bit deeper in its associations.
See the line that starts $this->set? Set does only one thing – it takes a variable name
(‘user’ in this case) and assigns a value to it. $this->User->read(null, $id) pulls one
record (with the matching id) from the user table and assigns it to the variable ‘user’ and
sends that variable to the view.
Now look in the views folder and find the users folder. Open view.ctp (note – if you use
the stable version of cake, this will be called view.thtml). Add this at the very bottom of
view.ctp:
<?php pr( $user ); ?>
The <?php ?> tags tell the server that everything inside should be processed on the
server, and only the results should be sent to the browser. pr($user) prints the
preformatted value of the $user variable. Remember, $user was assigned in the controller
and sent to the view. Since we put this at the bottom of the page, everything inside of
user will be printed at the bottom. Mine looks like this:
Array
(
[User] => Array
(
[id] => 1
[name] => Hunter, Rick
[address1] => Officer's Country
[address2] => Fokker Base
[city] => Macross City
[state] => SDF1
[zip] => 11111
[home_phone] => 555-1212
[cell_phone] => 555-2121
[birthday] => 1988-02-01
[school_id] => 1
[school_grade] => 11
[email_address] => rhunter@rdf.net
[myspace_name] => skull1
)
[School] => Array
(
[id] => 1
[school_name] => Bridgeport High School
[User] => Array
(
[0] => Array
(
[id] => 1
[name] => Hunter, Rick
[address1] => Officer's Country
[address2] => Fokker Base
[city] => Macross City
[state] => SDF1
[zip] => 11111
[home_phone] => 555-1212
[cell_phone] => 555-2121
[birthday] => 1988-02-01
[school_id] => 1
[school_grade] => 11
[email_address] => rhunter@rdf.net
[myspace_name] => skull1
)
)
)
[Registration] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[event_id] => 1
[permission_slip] => 0
[paid] => 0
[balance] => 0
[Event] => Array
(
[id] => 1
[event_name] => Creating WebApps with
CakePHP
[event_start] => 2007-02-08 10:00:00
[event_end] => 2007-02-08 11:30:00
[event_description] => Learn to write
webapps the easy way: with CakePHP!
[event_cost] => 0
[event_deposit] => 0
[event_deposit_due] => 2007-02-08
)
[User] => Array
(
[id] => 1
[name] => Hunter, Rick
[address1] => Officer's Country
[address2] => Fokker Base
[city] => Macross City
[state] => SDF1
[zip] => 11111
[home_phone] => 555-1212
[cell_phone] => 555-2121
[birthday] => 1988-02-01
[school_id] => 1
[school_grade] => 11
[email_address] => rhunter@rdf.net
[myspace_name] => skull1
)
)
[1] => Array
(
[id] => 4
[user_id] => 1
[event_id] => 3
[permission_slip] => 1
[paid] => 0
[balance] => 40
[Event] => Array
(
[id] => 3
[event_name] => Arkansas Spring Break Trip
[event_start] => 2007-03-12 10:00:00
[event_end] => 2007-03-16 20:00:00
[event_description] => Bible Study,
Canoeing, Hiking, and GeoCaching! What more could you ask for?
[event_cost] => 40
[event_deposit] => 10
[event_deposit_due] => 2007-02-28
)
[User] => Array
(
[id] => 1
[name] => Hunter, Rick
[address1] => Officer's Country
[address2] => Fokker Base
[city] => Macross City
[state] => SDF1
[zip] => 11111
[home_phone] => 555-1212
[cell_phone] => 555-2121
[birthday] => 1988-02-01
[school_id] => 1
[school_grade] => 11
[email_address] => rhunter@rdf.net
[myspace_name] => skull1
)
)
)
)
What we have here is a multidimensional array. It contains info about our user, but also
about school and registration information. Everything we need to replace the id numbers
with names is already on the page.
To fix things, find the section of the page that starts
< div class = "related" >
Scroll down a little farther, and you’ll find where all the magic happens:
<?php foreach ( $user [ 'Registration' ] as $registration ): ?>
< tr >
< td > <?php echo $registration [ 'id' ]; ?> </ td >
< td > <?php echo $registration [ 'user_id' ]; ?> </ td >
< td > <?php echo $registration [ 'event_id' ]; ?> </ td >
< td > <?php echo $registration [ 'permission_slip' ]; ?> </ td >
< td > <?php echo $registration [ 'paid' ]; ?> </ td >
< td > <?php echo $registration [ 'balance' ]; ?> </ td >
< td nowrap >
<?php echo $html ->link( 'View' , '/registrations/view/'
. $registration [ 'id' ]); ?>
<?php echo $html ->link( 'Edit' , '/registrations/edit/'
. $registration [ 'id' ]); ?>
<?php echo $html ->link( 'Delete' ,
'/registrations/delete/' . $registration [ 'id' ], null, 'Are you sure you
want to delete: id ' . $registration [ 'id' ] . '?' ); ?>
</ td >
</ tr >
<?php endforeach ; ?>
The lines we are looking for are here:
< td > <?php echo $registration [ 'user_id' ]; ?> </ td >
< td > <?php echo $registration [ 'event_id' ]; ?> </ td >
Since we are already looking at our user page, we can get rid of the line for user_id, and
also the <th>User ID</th> above it. Now replace:
Zgłoś jeśli naruszono regulamin