Last active
August 29, 2015 14:07
-
-
Save fullybaked/ebd9aeb75605e5a4d165 to your computer and use it in GitHub Desktop.
Scoped CakePHP Models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Person extends AppModel | |
{ | |
// must explicitly set the useTable so child classes | |
// use this not the CakePHP conventions based on their | |
// class name | |
public $useTable = 'people'; | |
// table people exists in the database with schema | |
// id int(11) PK | |
// type varchar(20) default 'contact' not null | |
// full_name varchar(255) | |
// known_as varchar(50) | |
// --- snip --- | |
} | |
// ------------- | |
App::uses('Person', 'Model'); | |
class Contact extends Person | |
{ | |
public function beforeFind($query) | |
{ | |
parent::beforeFind($query); | |
$query['conditions']['Contact.type'] = 'contact'; | |
return $query; | |
} | |
} | |
// ------------------------ | |
App::uses('Person', 'Model'); | |
class Agent extends Person | |
{ | |
public function beforeFind($query) | |
{ | |
parent::beforeFind($query); | |
$query['conditions']['Agent.type'] = 'agent'; | |
return $query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment