root/trunk/application/controllers/irtest.php

User picture

Author: m4rw3r

Revision: 279 («Previous)


File Size: 7.76 KB

(October 18, 2008 16:22 UTC) Over 3 years ago

Remade dbobj2ORM to be able to make JOINs and then split the data into many objects
Moved join code into relation objects

 
Show/hide line numbers
<?php 
/*
 * Created on 2008 Apr 30
 * by Martin Wernstahl <m4rw3r@gmail.com>
 */
/**
 * Customizeable print_r variant.
 *
 * Use only the $subject and $ignore parameters, the others are used internally
 *
 * @param $subject The object/variable/array to be printed
 * @param $ignore An array with the keys to ignore
 * @param $depth Current depth (used in recursion)
 * @param $refChain A chain of printed objects to avoid recursion
	 */
function u_print_r($subject, $ignore = array(), $depth = 1, $refChain = array()){
	$skip = false; // print skip?
	if ($depth > 20) return;
	if (is_object($subject)) {
		foreach ($refChain as $refVal)
			if (serialize($refVal) == serialize($subject)) {
				echo "*RECURSION*\n";
				return;
			}
		array_push($refChain, $subject);
		echo get_class($subject) . " Object ( \n";
		$subject = (array) $subject;
		foreach ($subject as $key => $val)
			if (is_array($ignore) && !in_array($key, $ignore, 1)) {
				echo str_repeat(" ", $depth * 4) . '[';
				if ($key{0} == "\0") {
					$keyParts = explode("\0", $key);
					echo $keyParts[2] . (($keyParts[1] == '*')  ? ':protected' : ':private');
				} else
					echo $key;
				echo '] => ';
				u_print_r($val, $ignore, $depth + 1, $refChain);
			}
			else{
				if($skip){
				// echoes skip when it skips an ignored value
				echo str_repeat(" ", $depth * 4)."[".$key."]: SKIP\n";
				}
			}
		echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
		array_pop($refChain);
	} elseif (is_array($subject)) {
		echo "Array ( \n";
		foreach ($subject as $key => $val){
			if (is_array($ignore) && !in_array($key, $ignore, 1)) {
				echo str_repeat(" ", $depth * 4) . '[' . $key . '] => ';
				u_print_r($val, $ignore, $depth + 1, $refChain);
			}
			else{
				if($skip){
					// echoes skip when it skips an ignored value
					echo str_repeat(" ", $depth * 4)."[".$key."]: SKIP\n";
				}
			}
		}
		echo str_repeat(" ", ($depth - 1) * 4) . ")\n";
	} else
		var_dump($subject) . "\n";
}
function u($subject,$ignore = array()){
	echo '<pre>';
	u_print_r($subject,$ignore);
	echo '</pre>';
}
class irtest extends Controller{
	function irtest(){
		parent::Controller();
		$this->load->database();
		$this->load->library('unit_test');
		//$this->unit->use_strict(TRUE);
		$this->output->enable_profiler(TRUE);
	}
	
	// --------------------------------------------------------------------
		
	/**
	 * 
	 */
	function validation()
	{
		if(!class_exists('IgnitedRecord')){
			// change this line if the IgnitedRecord file is stored elsewhere
			require_once(APPPATH.'models/IgnitedRecord/ignitedrecord.php');
		}
		
		$this->user = IgnitedRecord::factory('users', array('act_as' => array(
'validation' => array(
				'name' => array('rules' => 'trim|required|max_length[10]'),
				'password' => array('rules' => 'required|min_length[3]', 'label' => 'Secret')
				)

)));
		
		$u = $this->user->new_record();
		
		$u->name = 'lol';
		$u->password = 'ed';
		$u->validate();
		
		var_dump($this->user->errors());
	}
	
	
	// --------------------------------------------------------------------
		
	/**
	 * 
	 */
	function performance()
	{
		define('IR_USE_IQ',true);
		if(!class_exists('IgnitedRecord')){
			// change this line if the IgnitedRecord file is stored elsewhere
			require_once(APPPATH.'models/IgnitedRecord/ignitedrecord.php');
		}
		$this->benchmark->mark('IR_start');
		$this->user = IgnitedRecord::factory('users');
		$this->user->habtm('groups');
		for($i = 0; $i < 1000; $i++)
		{
			$o = $this->user->find(1);
			$r = $o->related('groups')->order_by('name', 'desc')->get();
		}
		$this->benchmark->mark('IR_end');
		/*$this->benchmark->mark('IR_start');
$this->user = IgnitedRecord::factory('users');
$this->user->habtm('groups');
$this->group = IgnitedRecord::factory('groups');
for($i = 0; $i < 10000; $i++)
{
	$o = $this->user->find(1);
	$r = $this->group->find_all_by_sql('SELECT * FROM `groups` WHERE `id` IN (SELECT `group_id` FROM `groups_users` WHERE `user_id` = "'.$o->id.'") ORDER BY `name` desc');
}
$this->benchmark->mark('IR_end');*/
		/*$this->benchmark->mark('AR_start');
		for($i = 0; $i < 1000; $i++){
			$o = $this->db->get_where('users',array('id' => 1))->row();
			$ids = $this->db->select('group_id')->from('groups_users')->where('user_id', $o->id)->get();
			foreach($ids->result() as $id)
			{
				$tmp[] = $id->group_id;
			}
			$r = $this->db->where_in('id', $tmp)->from('groups')->order_by('name', 'desc')->get()->result();
		}
		$this->benchmark->mark('AR_end');*/
	/*	$this->benchmark->mark('AR_start');
		for($i = 0; $i < 10000; $i++){
			$o = $this->db->get_where('users',array('id' => 1))->row();
			$r = $this->db->query('SELECT * FROM `groups` WHERE `id` IN (SELECT `group_id` FROM `groups_users` WHERE `user_id` = "'.$o->id.'") ORDER BY `name` desc')->result();
		}*
			$this->benchmark->mark('AR_end');*/
	/*	
$this->benchmark->mark('AR_start');
for($i = 0; $i < 10000; $i++)
{
	$o = $this->db->get_where('users',array('id' => 1))->result();
}
$this->benchmark->mark('AR_end');*/

	}
	
	// --------------------------------------------------------------------
		
	/**
	 *
	 */
	function bla()
	{
		define('IR_USE_IQ',true);
		if(!class_exists('IgnitedRecord')){
			// change this line if the IgnitedRecord file is stored elsewhere
			require_once(APPPATH.'models/IgnitedRecord/ignitedrecord.php');
		}
		$this->user = IgnitedRecord::factory('users');
		$this->user->habtm('groups');
		/*$this->ticket = IgnitedRecord::factory('tickets');
		$this->ticket->has_many('answers');
		$this->ticket->belongs_to('user');
		$this->answer = IgnitedRecord::factory('answers');
		$this->answer->belongs_to('answer');*/
		/*var_dump(isset($l));
		$l = array();
		var_dump(isset($l));
		$this->load->orm();
		$this->user = IgnitedRecord::factory('users',array('has_many' => 'tickets'));
		$this->ticket = IgnitedRecord::factory('tickets',array('belongs_to' => 'users','has_many' => 'answers'));
		$this->answer = IgnitedRecord::factory('answers',array('belongs_to' => 'tickets'));*/
		$this->user->join_related('groups');
		$user = $this->user->find_all();
		/*u($user);
		echo $this->db->last_query();
		/*foreach($user->related('tickets')->get() as $ticket)
		{
			u($ticket,array('__instance','__relations','__relation_properties','__data','mpttree','tree'));
		    foreach($ticket->get_related('answers') as $answer)
		    {
		        echo $answer->text;
		    }
		}*/
	}
	/**
	 * Test suite for IgnitedRecord.
	 */
	function index(){
		// set up test tables:
		/*$this->db->query(
'DROP TABLE IF EXISTS `test_posts`;
CREATE TABLE "test_posts" (
  "id" int(10) unsigned NOT NULL auto_increment,
  "text" varchar(300) NOT NULL,
  "test_users_id" int(10) unsigned NOT NULL,
  PRIMARY KEY  ("id")
) AUTO_INCREMENT=1 ;');
		$this->db->query(
'DROP TABLE IF EXISTS `test_users`;
CREATE TABLE "test_users" (
  "id" int(10) unsigned NOT NULL auto_increment,
  "name" varchar(200) NOT NULL,
  "email" varchar(200) NOT NULL,
  PRIMARY KEY  ("id")
	) AUTO_INCREMENT=1 ;');*/
		
		// start test
		$this->load->orm();
		$this->unit->run(class_exists('IgnitedRecord'),
						 true,'Loading of Ignitedrecord');

		$this->user = IgnitedRecord::yamlfactory(
'table: test_users
habtm:
	-name: lol
	 table: test_habtm
	 model: blaaaa
');
		$this->unit->run(isset($this->user) && $this->user instanceof IgnitedRecord,
						 true,'Creating a model with the yamlfactory() method');
		$this->unit->run($this->user->table,
						 'test_users','Checking __table is correctly set');
						 
						 
		$this->post = IgnitedRecord::factory('test_posts',
					array('belongs_to' => 'test_users',
						'habtm' => array('name' => 'lol','table' => 'test_habtm','model' => 'user')
					));
		$this->unit->run(isset($this->post) && $this->post instanceof IgnitedRecord,
						 true,'Creating a model with the factory() method');
		$this->unit->run($this->post->table,
						 'test_posts','Checking __table is correctly set');
		
		echo $this->unit->summary_report();
	}
}
?>