Thursday, November 29, 2012

Code Snippets for the FuelPHP Training in AdDU


return array(
'default' => array(
'connection'  => array(
'dsn' => 'mysql:host=localhost;dbname=shop',
'username'   => 'root',
'password'   => '',
),
),
);

set path=C:\wamp\bin\php\php5.3.13\

php oil generate model users username:varchar[50] password:string group:int email:string last_login:int login_hash:string profile_fields:text

php oil refine migrate

create users with different levels
Auth::create_user('admin', 'secret', 'philippineoutsourcing@gmail.com', 100);
Auth::create_user('guest', 'secret', 'guest@philippineglobaloutsourcing.com', 0);

Auth::create_user('user', 'secret', 'user@philippineglobaloutsourcing.com', 1);
Auth::create_user('moderator', 'secret', 'moderator@philippineglobaloutsourcing.com', 50);
Auth::create_user('user2', 'secret', 'user2@philippineglobaloutsourcing.com', 0);
Auth::create_user('banned', 'secret', 'banned@philippineglobaloutsourcing.com', -1);




oil generate admin products title:string slug:string summary:text body:text price:int:unsigned qty:int:unsigned user_id:int
php oil refine migrate


--- updating crud


public function action_create($id = null)
{
  $view = View::forge('admin/products/create');
  if (Input::method() == 'POST')
  {
 $product = Model_Product::forge(array(
'title' => Input::product('title'),
'slug' => Inflector::friendly_title(Input::product('title'), '-', true),
'summary' => Input::product('summary'),
'price' => Input::product('price'),
'qty' => Input::product('qty'),
'body' => Input::product('body'),
'user_id' => Input::product('user_id'),
 ));
 if ($product and $product->save())
 {
Session::set_flash('success', 'Added product #'.$product->id.'.');
Response::redirect('admin/products');
 }
 else
 {
Session::set_flash('error', 'Could not save product.');
 }
  }
  // Set some data
  $view->set_global('users', Arr::assoc_to_keyval(Model_User::find('all'), 'id', 'username'));
  $this->template->title = "Create Products";
  $this->template->content = $view;
}


public function action_edit($id = null)
{
  $view = View::forge('admin/products/edit');
  $product = Model_Product::find($id);
  if (Input::method() == 'POST')
  {
 $product->title = Input::product('title');
 $product->slug = Inflector::friendly_title(Input::product('title'), '-', true);
 $product->summary = Input::product('summary');
 $product->body = Input::product('body');
 $product->user_id = Input::product('user_id');
 if ($product->save())
 {
Session::set_flash('success', 'Updated product #' . $id);
Response::redirect('admin/products');
 }
 else
 {
Session::set_flash('error', 'Could not update product #' . $id);
 }
  }
  else
  {
 $this->template->set_global('product', $product, false);
  }
  // Set some data
  $view->set_global('users', Arr::assoc_to_keyval(Model_User::find('all'), 'id', 'username'));
  $this->template->title = "Edit Product";
  $this->template->content = $view;
}

view


<div class="clearfix">
<?php echo Form::label('User', 'user_id'); ?>
<div class="input">
<?php echo Form::select('user_id', Input::post('user_id', isset($product) ? $product->user_id : $current_user->id), $users, array('class' => 'span6')); ?>
</div>
</div>




fuel/app/classes/controller/products.php

class Controller_Products extends Controller_Base
{
   public function action_index()
   {
      $view = View::forge('products/index');
      $view->products = Model_Product::find('all');
      $this->template->title = 'My Products about Stuff';
      $this->template->content = $view;
   }
}

view


<h2>Recent Products</h2>
<?php foreach ($products as $product): ?>
   <h3><?php echo Html::anchor('product/view/'.$product->slug, $product->title) ?></h3>
   <p><?php echo $product->summary ?></p>
<?php endforeach; ?>




public function action_view($slug)
{
   $product = Model_Product::find_by_slug($slug);
   $this->template->title = $product->title;
   $this->template->content = View::forge('products/view', array(
      'product' => $product,
   ));
}

<h2><?php echo $product->title ?></h2>
<p><strong>Posted: </strong><?php echo date('nS F, Y', $product->created_at) ?> (<?php echo Date::time_ago($product->created_at)?>)</p>
<p><?php echo nl2br($product->body) ?></p>

model
protected static $_belongs_to = array('user');
protected static $_has_many = array('product');


$product = Model_Product::find('first');
$product->user->username


view

<h2><?php echo $product->title ?></h2>
<p>
   <strong>Posted: </strong><?php echo date('nS F, Y', $product->created_at) ?> (<?php echo Date::time_ago($product->created_at)?>)
   by <?php echo $product->user->username ?>
</p>
<p><?php echo nl2br($product->body) ?></p>



No comments:

Post a Comment