SitePoint Sponsor |
|
User Tag List
Results 126 to 150 of 171
-
Mar 4, 2006, 16:43 #126
Originally Posted by dreamscape
Sqlite, for example, will quite happily store a large blob into an integer column.
-
Mar 5, 2006, 00:23 #127
Originally Posted by Ren
Originally Posted by Ren
Originally Posted by Ren
Originally Posted by dreamscape
Originally Posted by dreamscape
It's not that I don't see your point dreamscape, I just don't agree with you.
-
Mar 5, 2006, 00:40 #128
Originally Posted by thr
-
Mar 5, 2006, 00:42 #129
Originally Posted by dreamscape
-
Mar 5, 2006, 01:07 #130
The current api would allow this:
PHP Code:<?php
// This does not allow null on givenName/familyName and adds them to constraint group 1
$template->givenName = Model::stringField(10, false, 1);
$template->familyName = Model::stringField(20, false, 1);
// This allows null on email and age, and adds them to constraint group 2
$template->email = Model::stringField(25, true, 2);
$template->age = Model::integerField(3, true, 2);
?>Code:create table `Person` ( `id` int( 11 ) not null , `firstName` varchar( 10 ) not null , `givenName` varchar( 10 ) not null , `email` varchar( 25 ) , `age` smallint( 3 ) , primary key ( `id` ) , unique ( `firstName` , `givenName` ), unique ( `email` , `age` ), ) engine = InnoDB;
On another note, should the "$allownull" be false or true as standard?Last edited by thr; Mar 5, 2006 at 01:38.
-
Mar 5, 2006, 03:10 #131
Ok, some more "status" updates - currently the ClassMaps handle inheritance flawlessly, take this example:
classes/Person.php:PHP Code:<?php
class Person implements Persistable
{
public $name;
public $email;
public $age;
public $groups;
protected $parent;
protected $savings;
public function setParent($val) { $this->parent = $val; }
public function getParent() { return $this->parent; }
public function setSavings($savings) { $this->savings = (double)$savings; }
public function getSavings() { return $this->savings; }
public function getNameEmailLink() { return "<a href='mailto{$this->email}'>{$this->name}</a>"; }
}
?>PHP Code:<?php
class Member extends Person
{
public $joindate;
}
?>PHP Code:<?php
class Admin extends Member
{
}
?>PHP Code:<?php
$template = new ModelClassMap("Person");
$template->name = Model::stringField(10, true);
$template->email = Model::stringField(25, true);
$template->age = Model::integerField(3);
$template->groups = Model::collectionField("Group");
$template->setSavings(Model::doubleField(100, true));
$template->setParent(Model::objectField("Person"));
?>PHP Code:<?php
$template = new ModelClassMap("Member");
$template->joindate = Model::integerField(11);
?>PHP Code:<?php
$template = new ModelClassMap("Admin");
$template->joindate = Model::integerField(11, true);
$template->email = Model::stringField(50, true);
?>
Admin then extends Member, but there is not a required joindate for admins and thus it changes the "allownull" for joindate to true. The email adress is not required either for the admin so he changes the field define in his parents(Member) parent(Person) to allow null.
This example works flawlessly, tests.php:PHP Code:<?php
require 'UnitTests/Helpers/dev_funcs.php';
require_all('./');
require_all('Interfaces/');
require_all('UnitTests/Classes/');
$cfg = new ModelConfiguration;
$cfg->database("mysql", "localhost", "development", "dev", "devnull");
$cfg->paths("UnitTests/Classes/", "UnitTests/Templates/", "UnitTests/Templates/Cache/");
$model = new Model($cfg);
$template = $model->loadClassMap("Person");
$person = new Person;
$person->name = null;
$person->email = "thrthr@gmail.com";
$person->age = 20;
$person->groups = array();
$person->setSavings(0.0);
$person->setParent(new Person);
$template->validateObject($person);
$template = $model->loadClassMap("Member");
$member = new Member;
$member->name = null;
$member->email = "thrthr@gmail.com";
$member->age = 20;
$member->groups = array();
$member->setSavings(0.0);
$member->setParent(new Person);
$member->joindate = 1;
$template->validateObject($member);
$template = $model->loadClassMap("Admin");
$admin = new Admin;
$admin->name = null;
$admin->age = 20;
$admin->groups = array();
$admin->setSavings(0.0);
$admin->setParent(new Person);
$admin->joindate = 1;
$template->validateObject($admin);
?>
-
Mar 5, 2006, 04:47 #132
- Join Date
- Sep 2003
- Location
- Bratislava, Slovakia
- Posts
- 184
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've played around with lastcrafts idea of "magic" declarative definitions.
PHP Code:class Node {
protected $id;
protected $sid;
protected $parentId;
protected $position;
protected $visible;
public static function getMapping() {
$mapping = new ClassMapping('nodes');
$mapping->id->primaryKey;
$mapping->sid->string;
$mapping->parentId('parent_id')->null->default(null);
$mapping->visible->boolean->default(true);
$mapping->setUniqueKey('parentId', 'sid');
return $mapping;
}
}
class Article extends Node {
protected $publishedOn;
protected $title;
protected $body;
protected $authors;
public static function getMapping() {
$mapping = new ExtendingClassMapping('articles');
$mapping->title->string;
$mapping->body->string;
$mapping->publishedOn('published_on')->custom(new DateFieldMapping())->null->default(null);
return $mapping;
}
}
class Authorship {
protected $articleId;
protected $authorId;
public static function getMapping() {
$mapping = new ClassMapping('authorships');
$mapping->articleId('article_id');
$mapping->authorId('author_id');
$mapping->setPrimaryKey('articleId', 'authorId');
return $mapping;
}
}
Annotations support for PHP5
TC/OPT™ Group Leader
-
Mar 5, 2006, 04:54 #133
I think it's way to messy and I prefer to have set Classes/Objects to handle field mapping, such as in my last post. I played around with lastcrafts idea also but It just get's a bit to when you say wan't to add a primary key,
$mapping->id->primarykey->not_null->auto_increment->integer;
that's just like writing Sql ;p
-
Mar 5, 2006, 13:45 #134
- Join Date
- Sep 2003
- Location
- Bratislava, Slovakia
- Posts
- 184
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by thr
PHP Code:$template = new ModelClassMap("Person");
$template->name = Model::stringField(10, true);
$template->email = Model::stringField(25, true);
$template->age = Model::integerField(3);
$template->groups = Model::collectionField("Group");
PHP Code:$template = new ModelClassMap("Person");
$template->name->string(10)->null;
$template->email->string(25)->null;
$template->age->integer(3);
$template->groups->collection("Group");
Annotations support for PHP5
TC/OPT™ Group Leader
-
Mar 6, 2006, 03:27 #135
Originally Posted by johno
-
Mar 6, 2006, 05:35 #136
Ok, new idea:
PHP Code:<?php
class TestModel extends Model
{
protected function PersonMap()
{
$person = $this->createMapping("Person");
$person->name->string(25)->notNull();
$person->age->integer(11)->notNull();
$person->parent->object("Person");
$person->groups->collection("Group");
}
protected function GroupMap()
{
$group = $this->createMapping("Group");
$group->name->string(25)->notNull();
$group->comment->string(255);
$group->members->collection("Person");
}
}
?>
Idea is this: You have your basic "abstract class Model" which is the top level in the model heriarchy - to use objects with a model you extend it and define methods in your class named "ClassMap();" (as you can see above) which should be protected - this would allow you to builda heirarchy of model classes for example Module-based applications, etc. To hide a type of mapping from a subclass just make the method private instead.
This would also mean you have all your mappings in one single place.
-
Mar 6, 2006, 05:52 #137
- Join Date
- Oct 2004
- Location
- Worcester
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by thr
I think in terms of modules for my sites. A module consists of the table definition, the php code and the html templates. Thus, I'd quite like to keep everything related to the module together so it's easy to take to another site.
-
Mar 6, 2006, 06:16 #138
Think I'd split it.
PHP Code:
interface ModelMapContainer
{
function getMap(Model $model, $name);
}
class SimpleModelMapContainer implements ModelMapContainer
{
private $parent;
function __construct(ModelMapContainer $parent = null)
{
$this->parent = $parent;
}
function getMap(Model $model, $name)
{
$methodName = $name.'Map';
if (method_exists($this, $methodName))
return $this->$methodName();
if ($this->parent instanceof ModelMapContainer)
return $this->parent->getMap($model, $name);
return NULL;
}
function PersonMap()
{
...
}
}
class Model
{
protected $maps;
function __construct(ModelMapContainer $mapContainer)
{
$this->mapContainer = $mapContainer;
}
function getMap($name)
{
if (!isset($this->maps[$name]))
$this->maps[$className] = $this->mapContainer->getMap($this, $className);
return $this->maps[$name];
}
}
-
Mar 6, 2006, 06:41 #139
Nice idea Ren, I agree that's a much better approach. (this is the exact reason I don't code everything and then release it as "this is done", perfer to debate / argue everything with others before making a decision)
Originally Posted by Ren
-
Mar 6, 2006, 07:10 #140
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sorry for going off topic Thr, but you shouldn't post script that uses bad practices, and yes I know I'm just being picky
PHP Code:public function getNameEmailLink() { return "<a href='mailto{$this->email}'>{$this->name}</a>"; }
}
PHP Code:public function getNameEmailLink( PersonPrinter $printer ) {
return $printer -> formatEmail( $this -> email, $this -> name );
}
}
PHP Code:// ...
public function toHtml() {
return new PersonFormat( $this -> export() );
}
// ...
class PersonFormat {
// ...
public function getName() {
return $this -> lastname.' '.$this -> firstname;
}
// ...
}
$formatter = $person -> toHtml();
$name = $formatter -> getName();
-
Mar 6, 2006, 07:13 #141
haha, ah well
sorry for that ;p I just threw in a method that had get as prefix to demonstrate that it only takes valid set/get pairs.
-
Mar 6, 2006, 07:16 #142
Originally Posted by thr
PHP Code:class B
{
}
class A
{
private $b;
function __construct(B $b = null)
{
$this->b = $b;
}
}
$a = new A();
var_dump($a);
Code:object(A)#1 (1) { ["b:private"]=> NULL }
-
Mar 6, 2006, 07:20 #143
got 5.1.2 also, gotta try again then ;P
-
Mar 6, 2006, 07:23 #144
back to topic, anyways - I realy don't like using __set/__get magic if it can be avoided so here's my current version of what Ren wrote above:
PHP Code:<?php
interface MMapContainer{
public function getMap($name);
}
class MClassMethodMap implements MMapContainer{
function getMap($method){
$method = (string)$method . 'Map';
if(method_exists($this, $method)){
return $this->$method();
}else{
return null;
}
}
function PersonMap(){
$map = new Person;
$map->name = "string:25";
$map->age = "integer:3";
$map->text = "text:long";
$map->groups = array(new Group);
$map->children = array(new Person);
$map->parent = new Person;
}
}
?>
Good, bad, ugly?
-
Mar 6, 2006, 07:26 #145
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Umm...
I have 5.1.1 at the moment, but I only ask a question though,
PHP Code:function __construct(B $b = null)
{
$this->b = $b;
}
I just don't see the point or any benifit in giving a class method arguement a default, particularly when there is the use of type hints for enforcement; Or am I missing something?
-
Mar 6, 2006, 07:29 #146
Originally Posted by Dr Livingston
-
Mar 6, 2006, 07:38 #147
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes, but NULL isn't the type expected no? Say... I must be thick this morning
-
Mar 6, 2006, 07:41 #148
Originally Posted by Dr Livingston
-
Mar 6, 2006, 07:49 #149
Hmm few methods on Model? Seems to make sense.
getType($type, $allowsNull = true, $length = null, $precision = null)
getHasOneType('Person' .... );
getHasManyType('Groups', .... );
getValueType('', ...columns... );
PHP Code:$person->name = $model->getType('string', false, 10);
$person->age = $model->getType('integer', false);
-
Mar 6, 2006, 07:51 #150
yeah you're probably right in that case, the approach I had above but with statics, it'll probably be best that way.
Bookmarks