summaryrefslogtreecommitdiff
path: root/tests/simple_unit/ActiveRecord/ForeignKeyTestCase.php
blob: 6c240b2f78158537c57ce6d04d93b8be28954142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

Prado::using('System.Data.ActiveRecord.TActiveRecord');

abstract class Mysql4Record extends TActiveRecord
{
	protected static $conn;

	public function getDbConnection()
	{
		if(self::$conn===null)
			self::$conn = new TDbConnection('mysql:host=localhost;port=3306;dbname=tests', 'test4', 'test4');
		return self::$conn;
	}
}

class Album extends Mysql4Record
{
	public $title;

	public $Tracks = array(self::HAS_MANY, 'Track');
	public $Artists = array(self::HAS_MANY, 'Artist', 'album_artist');

	public static function finder($class=__CLASS__)
	{
		return parent::finder($class);
	}
}

class Artist extends Mysql4Record
{
	public $name;

	public $Albums = array(self::HAS_MANY, 'Album', 'album_artist');

	public static function finder($class=__CLASS__)
	{
		return parent::finder($class);
	}
}

class Track extends Mysql4Record
{
	public $id;
	public $song_name;
	public $album_id; //FK -> Album.id

	public $Album = array(self::BELONGS_TO, 'Album');

	public static function finder($class=__CLASS__)
	{
		return parent::finder($class);
	}
}

abstract class SqliteRecord extends TActiveRecord
{
	protected static $conn;

	public function getDbConnection()
	{
		if(self::$conn===null)
			self::$conn = new TDbConnection('sqlite:'.dirname(__FILE__).'/blog.db');
		return self::$conn;
	}
}

class PostRecord extends SqliteRecord
{
	const TABLE='posts';
	public $post_id;
	public $author;
	public $create_time;
	public $title;
	public $content;
	public $status;

	public $authorRecord = array(self::HAS_ONE, 'BlogUserRecord');

	public static function finder($className=__CLASS__)
	{
		return parent::finder($className);
	}
}
class BlogUserRecord extends SqliteRecord
{
	const TABLE='users';
	public $username;
	public $email;
	public $password;
	public $role;
	public $first_name;
	public $last_name;

	public $posts = array(self::HAS_MANY, 'PostRecord');

	public static function finder($className=__CLASS__)
	{
		return parent::finder($className);
	}
}

class ForeignKeyTestCase extends UnitTestCase
{
	function test()
	{
		$album = Album::finder()->withTracks()->findAll();
		//print_r($album);
		//print_r(PostRecord::finder()->findAll());
		//print_r(BlogUserRecord::finder()->with_posts()->findAll());
	}
}

?>