Skip to content

Instantly share code, notes, and snippets.

@n-kaito
Last active December 19, 2015 04:39
Show Gist options
  • Save n-kaito/15617119ec83e65a524b to your computer and use it in GitHub Desktop.
Save n-kaito/15617119ec83e65a524b to your computer and use it in GitHub Desktop.

Create tables

CREATE TABLE `parents` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `created_at` int(11) DEFAULT NULL,
  `updated_at` int(11) DEFAULT NULL,
  `deleted_at` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `children` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `parent_id` int(11) unsigned NOT NULL,
  `created_at` int(11) DEFAULT NULL,
  `updated_at` int(11) DEFAULT NULL,
  `deleted_at` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Insert Data

INSERT INTO parents (name, created_at, updated_at) VALUES ('Paremt A', now(), now());
INSERT INTO children (parent_id, name, created_at, updated_at) VALUES (1, 'Child A-1', now(), now());

Create models

<?php
//fuel/app/classes/model/parent.php

class Model_Parent extends \Orm\Model_Soft
{
  protected static $_properties = array(
		'id',
		'name',
		'deleted_at' => array('default' => null),
		'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,
		),
	);
  
  protected static $_table_name = 'parents';
  protected static $_has_many = array('children');  
}
<?php
// fuel/app/classes/model/child.php
class Model_Child extends \Orm\Model_Soft
{
    protected static $_properties = array(
        'id',
        'name',
        'parent_id',
        'deleted_at' => array('default' => null),
        '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_update'),
            'mysql_timestamp' => false,
        ),
    );
    protected static $_table_name = 'children';
    protected static $_belongs_to = array('parent');
}

Delete Child Data (not reference belongs_to data)

<?php
//fuel/app/classes/controller/child.php 
  
  // default function when you use scaffold
  public function action_delete($id = null)
  {
    is_null($id) and Response::redirect('child');

    if ($child = Model_Child::find($id)) {
      $child->delete();
      Session::set_flash('success', 'Deleted child #'.$id);
    } else {
      Session::set_flash('error', 'Could not delete child #'.$id);
    }
    Response::redirect('child');
  }

Delete And Check Children Data (not reference belongs_to data)

id parent_id name deleted_at created_at updated_at
1 1 Child A-1 123456 123456 123456

Edit Delete code(reference belongs_to data)

<?php
//fuel/app/classes/controller/child.php 
  
  // default function when you use scaffold
  public function action_delete($id = null)
  {
    ・・・・・・・・ 
    if ($child = Model_Child::find($id)) {
      echo $child->parent->name;  // <---------------- Add this line
      $child->delete();
    ・・・・・・・・
  }

Delete And Check Children Data (reference belongs_to data)

id parent_id name deleted_at created_at updated_at
1 0 Child A-1 123456 123456 123456

result of parent_id is different when I use lazy loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment