Skip to content

Instantly share code, notes, and snippets.

@fullybaked
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fullybaked/ebd9aeb75605e5a4d165 to your computer and use it in GitHub Desktop.
Save fullybaked/ebd9aeb75605e5a4d165 to your computer and use it in GitHub Desktop.
Scoped CakePHP Models
<?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