Skip to content

Instantly share code, notes, and snippets.

@moakdesigns
Last active Aug 29, 2015
Embed
What would you like to do?
FuelPHP ORM Relations
<?php
class Controller_Admin_Clients extends Controller_Admin{
public function action_index()
{
$data['clients'] = Model_Client::find('all');
$this->template->title = "Clients";
$this->template->subtitle = "All Clients";
$this->template->content = View::forge('admin\clients/index', $data);
}
public function action_view($id = null)
{
$data['client'] = Model_Client::find($id);
$this->template->title = "Client";
$this->template->subtitle = "Profile";
$this->template->content = View::forge('admin\clients/view', $data);
}
public function action_create()
{
if (Input::method() == 'POST')
{
$val = Model_Client::validate('create');
if ($val->run())
{
$client = Model_Client::forge(array(
'company' => Input::post('company'),
'website' => Input::post('website'),
));
if ($client and $client->save())
{
Session::set_flash('success', e('Added client: '.$client->company.'.'));
Response::redirect('admin/clients');
}
else
{
Session::set_flash('error', e('Could not save client.'));
}
}
else
{
Session::set_flash('error', $val->error());
}
}
$this->template->title = "Clients";
$this->template->subtitle = "Creating New Client";
$this->template->content = View::forge('admin\clients/create');
}
public function action_edit($id = null)
{
$client = Model_Client::find($id);
$val = Model_Client::validate('edit');
if ($val->run())
{
$client->site = Input::post('company');
$client->web_address = Input::post('website');
if ($client->save())
{
Session::set_flash('success', e('Updated client ' . $company));
Response::redirect('admin/clients');
}
else
{
Session::set_flash('error', e('Could not update client ' . $company));
}
}
else
{
if (Input::method() == 'POST')
{
$client->site = $val->validated('company');
$client->web_address = $val->validated('website');
Session::set_flash('error', $val->error());
}
$this->template->set_global('client', $client, false);
}
$this->template->title = "Clients";
$this->template->subtitle = "Editing Client";
$this->template->content = View::forge('admin\clients/edit');
}
public function action_delete($id = null)
{
if ($client = Model_Client::find($id))
{
$client->delete();
Session::set_flash('success', e('Deleted client '.$company));
}
else
{
Session::set_flash('error', e('Could not delete client #'.$company));
}
Response::redirect('admin/clients');
}
}
<?php
class Controller_Admin_Contacts extends Controller_Admin{
public function action_index()
{
$data['contacts'] = Model_Contact::find('all');
$this->template->title = "Contacts";
$this->template->subtitle = "Viewing All Contacts";
$this->template->content = View::forge('admin\contacts/index', $data);
}
public function action_view($id = null)
{
$data['contact'] = Model_Contact::find($id);
$this->template->title = "Contact";
$this->template->subtitle = "Information";
$this->template->content = View::forge('admin\contacts/view', $data);
}
public function action_create()
{
if (Input::method() == 'POST')
{
$val = Model_Contact::validate('create');
if ($val->run())
{
$contact = Model_Contact::forge(array(
'first_name' => Input::post('first_name'),
'last_name' => Input::post('last_name'),
'phone' => Input::post('phone'),
'email' => Input::post('email'),
'modified_date' => Input::post('modified_date'),
'modified_by' => Input::post('modified_by'),
));
if ($contact and $contact->save())
{
Session::set_flash('success', e('Added contact #'.$contact->id.'.'));
Response::redirect('admin/contacts');
}
else
{
Session::set_flash('error', e('Could not save contact.'));
}
}
else
{
Session::set_flash('error', $val->error());
}
}
$this->template->title = "Contacts";
$this->template->subtitle = "Create";
$this->template->content = View::forge('admin\contacts/create');
}
public function action_edit($id = null)
{
$contact = Model_Contact::find($id);
$val = Model_Contact::validate('edit');
if ($val->run())
{
$contact->first_name = Input::post('first_name');
$contact->last_name = Input::post('last_name');
$contact->phone = Input::post('phone');
$contact->email = Input::post('email');
$contact->modified_date = Input::post('modified_date');
$contact->modified_by = Input::post('modified_by');
if ($contact->save())
{
Session::set_flash('success', e('Updated contact #' . $id));
Response::redirect('admin/contacts');
}
else
{
Session::set_flash('error', e('Could not update contact #' . $id));
}
}
else
{
if (Input::method() == 'POST')
{
$contact->first_name = $val->validated('first_name');
$contact->last_name = $val->validated('last_name');
$contact->phone = $val->validated('phone');
$contact->email = $val->validated('email');
$contact->modified_date = $val->validated('modified_date');
$contact->modified_by = $val->validated('modified_by');
Session::set_flash('error', $val->error());
}
$this->template->set_global('contact', $contact, false);
}
$this->template->title = "Contacts";
$this->template->subtitle = "Editing";
$this->template->content = View::forge('admin\contacts/edit');
}
public function action_delete($id = null)
{
if ($contact = Model_Contact::find($id))
{
$contact->delete();
Session::set_flash('success', e('Deleted contact #'.$id));
}
else
{
Session::set_flash('error', e('Could not delete contact #'.$id));
}
Response::redirect('admin/contacts');
}
}
<?php
class Model_Client extends \Orm\Model
{
protected static $_properties = array(
'id',
'company',
'website',
'created_at',
'updated_at',
);
protected static $_observers = array(
'Orm\Observer_CreatedAt' => array(
'events' => array('before_insert'),
'mysql_timestamp' => false,
),
'Orm\Observer_UpdatedAt' => array(
'events' => array('before_save'),
'mysql_timestamp' => false,
),
);
public static function validate($factory)
{
$val = Validation::forge($factory);
$val->add_field('company', 'Company Name', 'required|max_length[255]');
$val->add_field('website', 'Website', 'required|max_length[255]');
return $val;
}
protected static $_many_many = array('contacts');
}
<?php
class Model_Contact extends \Orm\Model
{
protected static $_properties = array(
'id',
'first_name',
'last_name',
'phone',
'email',
'modified_date',
'modified_by',
'created_at',
'updated_at',
);
protected static $_observers = array(
'Orm\Observer_CreatedAt' => array(
'events' => array('before_insert'),
'mysql_timestamp' => true,
),
'Orm\Observer_UpdatedAt' => array(
'events' => array('before_save'),
'mysql_timestamp' => true,
),
);
public static function validate($factory)
{
$val = Validation::forge($factory);
$val->add_field('first_name', 'First Name', 'required|max_length[255]');
$val->add_field('last_name', 'Last Name', 'required|max_length[255]');
$val->add_field('phone', 'Phone', 'required|max_length[255]');
$val->add_field('email', 'Email', 'required|valid_email|max_length[255]');
$val->add_field('modified_date', 'Modified Date', 'required');
$val->add_field('modified_by', 'Modified By', 'required|max_length[255]');
return $val;
}
protected static $_many_many = array('clients');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment