Matthew Machuga Mon Nov 01 00:58:40 -0400 2010
Perhaps using aliased attributes will be solve your problem. Could rename it to 'machine_type_name' or something similar. http://www.phpactiverecord.org/projects/main/wiki/Utilities#aliased-attributes
chris davis Mon Nov 01 09:27:44 -0400 2010
tried, but couldn't get it working .. based on my code above, what syntax would you use?
Does my relationship model look ok?
Kien La Tue Nov 02 13:13:18 -0400 2010
Serialization does not look at aliased attributes.
I find it hard to see why your parser couldn't handle this situation but it's hard to say without seeing how you are parsing it on the client side. Anyway, if you really wanted to rename it in the xml you could do the following:
1 class MachineType extends \ActiveRecord\Model {
2 public function machine_type_name() { return $this->name; }
3 }
4
5 Machine::first()->to_xml(array('include' =>
6 array('machine_type' => array(
7 'except' => 'name',
8 'methods' => array('machine_type_name')
9 )
10 )));
This is pretty bad but it should work.
(1-3/3)
Subject: trouble with same column name in has_one relationship
hi,
I'm no SQL expert, so this may be obvious.
Also, if I'm just doing this all wrong from a data model perspective, please advise .. totally new to this stuff and am trying to learn. Thanks in advance
I have a machine model that "has_one" machine_type...both of which have a "name" column.
I'm trying to figure out a way to serialize to xml and have the machine_type.name aliased as a different XML element and exist as a child of the root "machine" element instead of existing as a child of the machine_type.
I can to_xml() with the include attribute and see the machine_type show up, but my XML parser on the client can't distinguish between the two names
<machine>
<name>
<machine_type>
<name>
</machine_type>
</machine>
my tables look like this
CREATE TABLE IF NOT EXISTS `machine_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1000;
CREATE TABLE IF NOT EXISTS `machines` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`organization_id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`type_id` int(11) NOT NULL,
`make` varchar(50) NOT NULL,
`model` varchar(50) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`createdby` int(11) NOT NULL DEFAULT '0',
`modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`modifiedby` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
FOREIGN KEY (`organization_id`) references organizations(`id`),
FOREIGN KEY (`type_id`) references machine_types(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1000;
my models look like this :
class Machine extends \ActiveRecord\Model {
static $has_one = array(
array('type','class_name' => 'MachineType','primary_key' => 'type_id','foreign_key' => 'id')
);
}
class MachineType extends \ActiveRecord\Model {
}