Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]Stolz[/eluser]
[quote author="WanWizard" date="1358519144"]@Stolz,

What does the Orderitem model look like?

The count query generated is created on on instance of this model, and therefore uses that models relation definitions. So my assumption at the moment is that there is something wrong with the relationship definition from orderitem to order, causing the incorrect SQL.[/quote]

@WanWizard, thaks again for the support. Here are both models:

Code:
/*SQL:
CREATE TABLE IF NOT EXISTS orders (
id mediumint(4) unsigned NOT NULL auto_increment PRIMARY KEY,
user_id mediumint(1) unsigned, FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE RESTRICT,
orderstatus_id tinyint(1) unsigned, FOREIGN KEY (orderstatus_id) REFERENCES orderstatuses(id) ON UPDATE CASCADE ON DELETE RESTRICT,
place_id tinyint(1) unsigned, FOREIGN KEY (place_id) REFERENCES places(id) ON UPDATE CASCADE ON DELETE SET NULL,
paymenttype_id tinyint(1) unsigned, FOREIGN KEY (paymenttype_id) REFERENCES paymenttypes(id) ON UPDATE CASCADE ON DELETE SET NULL,
total float NOT NULL DEFAULT 0
) ENGINE = InnoDB;
*/
class Order extends DataMapper {

public $has_one = array('user', 'orderstatus', 'place',  'paymenttype');
public $has_many = array('orderitem');

public $validation = = array(
  'total' => array(
   'label' => 'Total',
   'rules' => array('trim', 'numeric', 'greater_than_equal_to' => 0)
  ),

  //Relationships validation

  'paymenttype' => array(
   'label' => 'Payment',
  ),

  'orderstatus' => array(
   'label' => 'Status',
   'rules' => array('required')
  ),

  'place' => array(
   'label' => 'Place',
   'rules' => array('required')
  ),

  'user' => array(
   'label' => 'User',
   'rules' => array('required')
  ),

  'orderitem' => array(
   'label' => 'Items',
   'rules' => array('required')
  ),
);

...


Code:
/*SQL:
CREATE TABLE IF NOT EXISTS orderitems (
id int(1) unsigned NOT NULL auto_increment PRIMARY KEY,
order_id mediumint(1) unsigned, FOREIGN KEY (order_id) REFERENCES orders(id) ON UPDATE CASCADE ON DELETE CASCADE,
orderitemstatus_id tinyint(1) unsigned, FOREIGN KEY (orderitemstatus_id) REFERENCES orderitemstatuses(id) ON UPDATE CASCADE ON DELETE RESTRICT,
name varchar(128) NOT NULL,
price float NOT NULL,
) ENGINE = InnoDB;
*/

class Orderitem extends DataMapper {

public $has_one = array('order', 'orderitemstatus');
public $has_many = array('orderitemextra');

public $validation = array(
  'name' => array(
   'label' => 'Name',
   'rules' => array('trim', 'required', 'max_length' => 128)
  ),

  'price' => array(
   'label' => 'Price',
   'rules' => array('trim', 'required', 'numeric', 'greater_than_equal_to' => 0)
  ),

  //Relationships validation

  'order' => array(
   'label' => 'Order',
   'rules' => array('required')
  ),

  'orderitemstatus' => array(
   'label' => 'Status',
   'rules' => array('required')
  ),
);
...

[eluser]WanWizard[/eluser]
@Maglok,

Don't thank me, thank 'jonahbron', he has send me a PR with the fix... Smile

[eluser]WanWizard[/eluser]
@Stolz,

Looks like a bug somewhere, as these models are pretty simple, and correctly defined. Problem is I don't have a look of time to look into it at the moment.

You are on the latest version? If not, pull it from bitbucket (https://bitbucket.org/wanwizard/datamapper/downloads, download 'tip'), and see if you still have the issue.

p.s. I see you're using ON DELETE CASCADE on your tables. Know that this doesn't play ball very well with Datamapper, you might run into issues when disconnecting related objects.

[eluser]Stolz[/eluser]
[quote author="WanWizard" date="1358592904"]@Stolz,

Looks like a bug somewhere, as these models are pretty simple, and correctly defined. Problem is I don't have a look of time to look into it at the moment.

You are on the latest version? If not, pull it from bitbucket (https://bitbucket.org/wanwizard/datamapper/downloads, download 'tip'), and see if you still have the issue.

p.s. I see you're using ON DELETE CASCADE on your tables. Know that this doesn't play ball very well with Datamapper, you might run into issues when disconnecting related objects.[/quote]

WanWizard,

I was using the CI sparks version. Now I've updated it to the 'tip' version from Bitbucket and the error is still there. I know you must be a busy man, no worry about it, I will find a (not elegant) workaround Smile.


Changing topic... I saw a few datamapper autogenerated queries that are supposed to expect/affect only one row from a table but they don't limit the operation. AFAIK that can become really annoying in huge databases. I don't know if it's a desired behaviour or not and I'm not complaining or asking for modifications. I don't even know if this has been discussed already, I'm just wondering.

For example:

Code:
$o = new Order(55);
SELECT * FROM (`orders`) WHERE `orders`.`id` =  55

¿shouldn't it be more convenient something like this?
Code:
SELECT * FROM (`orders`) WHERE `orders`.`id` =  55  LIMIT 1

Same for
Code:
$o->delete();
DELETE FROM `orders` WHERE `id` =  3
It coudl be
Code:
DELETE FROM `orders` WHERE `id` =  3  LIMIT 1

I can see sometimes you may don't want to limit the delete operation (i.e. when $o->all is populated) but then something like $o->limit(1)->delete(); shoudl be allowed but right now seems to have no limiting effect.

[eluser]Technoh[/eluser]
I have used Datamapper ORM in the past and now I am using it again with a new project, where sadly I have no control over the database. The fields are named in a certain way and I cannot change them. I have problems with the following tables (I used my copy of the main server):

Code:
CREATE TABLE IF NOT EXISTS `utilisateurs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nom_usager` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `numero_fiche` int(11) NOT NULL,
  `courriel` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `mot_passe` char(64) COLLATE utf8_unicode_ci NOT NULL,
  `sel` char(64) COLLATE utf8_unicode_ci NOT NULL,
  `prenom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `nom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `id_type` tinyint(4) NOT NULL,
  `id_etat` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `numero_fiche` (`numero_fiche`),
  KEY `id_type` (`id_type`),
  KEY `id_etat` (`id_etat`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;

Code:
CREATE TABLE IF NOT EXISTS `types_utilisateurs` (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;

Code:
CREATE TABLE IF NOT EXISTS `traductions_types_utilisateurs` (
  `id` smallint(6) NOT NULL AUTO_INCREMENT,
  `id_type_utilisateur` tinyint(4) NOT NULL,
  `id_langue` int(11) NOT NULL,
  `texte` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_type_utilisateur` (`id_type_utilisateur`,`id_langue`),
  KEY `id_etat_adresse` (`id_type_utilisateur`),
  KEY `langue` (`id_langue`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

An utilisateur has one type_utilisateur, identified by id_type, and every type_utilisateur has one or more traduction_type_utilisateur (linked via the id_type_utilisateur field. Disregarding validation rules since I already have those working well, how would I go about setting up the models' relationships? I've been trying to get the texte field from the traduction_type_utilisateur associated with the type_utilisateur, given a certain id_langue but I can't seem to wrap my head about the relationship configuration. The $table variable is configured correctly on all models.

I know Datamapper appends _id to fields and in my case it should be id_ before the field; I'm not looking for help with that, I'm looking for help with setting up the relationships and the exact code needed to read a field inside a deep relationship.

Any help is welcome!

[eluser]mkultra[/eluser]
Hello!

New on the forums here. I seriously need some help. Let me briefly describe what I'd like to do, and what I've tried in order to accomplish it:

I need Datamapper ORM to work for me; it does, but many-to-many relationships do not. So, I've tried everything. I've re-read the docs many many times over, double checked config's, double checked spelling of classes, models, columns, tables etc. to no avail.

I have tried many previous versions of DM, I've used the latest from GitHub (ty, btw), I've even gone back to try DMZ. Not only that, I've try these various versions on a blank CI install, CI 2.1.3 (which is the core we develop on), CI 2.1, CI 2.0.3. I've used it with HMVC, and non-HMVC.

No matter what I try, or what server I use it on, many-to-many does not work. I'm out of options and I'm desperately reaching out to you, whoever will help me.

I've developed a simple test case for all of these different cases using two simple models:

Cars and Wheels.

Code:
class Car extends DataMapper {

var $has_one = array();
var $has_many = array('wheel');
var $validation = array();

}

Code:
class Wheel extends DataMapper {

var $has_one = array();
var $has_many = array('car');
var $validation = array();

}

Pretend the appropriate constructor is in there...

The tables are just as simple:

Code:
CREATE TABLE `cars` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text collate latin1_general_ci,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

Code:
CREATE TABLE `wheels` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `name` text collate latin1_general_ci,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

Code:
CREATE TABLE `cars_wheels` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `car_id` int(11) unsigned default NULL,
  `wheel_id` int(11) unsigned default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

One of the strangest things that occurs to me that I know for sure something is wrong is that I can destroy "cars_wheels" table, and DM doesn't complain.

When I run my test function...

Code:
public function test()
{
  $wheel = new Wheel();
  $wheel->name = 'Goodyear';
  $wheel->save();
  
  $car = new Car();
  $car->name = 'Car';
  $car->wheel= $wheel;
  $car->save();
}

...it saves both entries, and no relationship. Please... please guide me to the light that I know is so close at hand Smile

[eluser]WanWizard[/eluser]
@stolz,

In general the RDBMS engine sql optimizer is smart enough to know that if you select by field value, and that field has a unique index, to stop searching once it has found the value in the index.

So I don't expect the absence of LIMIT to be a problem.

Also, you have to be a bit careful with LIMIT, because not every RDBMS supports that, and I don't want to limit the use of Datamapper to MySQL only.

[eluser]WanWizard[/eluser]
@Technoh

I think you have a problem. At present, Datamapper requires all foreign key fields to be suffixed with "_id". This is hardcoded, so you can't work around that.

It is on my (very long) todo list, but since I haven't written any CI code professionally anymore since 2010 (our company doesn't use CI anymore), it's not very high on that list.

[eluser]WanWizard[/eluser]
[quote author="mkultra" date="1359097063"]Hello!
When I run my test function...

Code:
public function test()
{
  $wheel = new Wheel();
  $wheel->name = 'Goodyear';
  $wheel->save();
  
  $car = new Car();
  $car->name = 'Car';
  $car->wheel= $wheel;
  $car->save();
}

...it saves both entries, and no relationship. Please... please guide me to the light that I know is so close at hand Smile[/quote]

And this is where you go wrong.

You create a relationship by linking both objects. By doing
Code:
$car->wheel= $wheel;
you just create a property called 'wheel' on the Car object, which doesn't do anything.

Instead, use
Code:
public function test()
{
  $wheel = new Wheel();
  $wheel->name = 'Goodyear';
  $wheel->save();
  
  $car = new Car();
  $car->name = 'Car';
  $car->save($wheel);
}

This will save the Car record, and relate it to the already existing Wheel record.

[eluser]mkultra[/eluser]
Dear WanWizard,

You are a genius! I knew I was doing something wrong, and I am so thankful that you have pointed it out for me. I have literally spent way too much time attempting to get this to work; in the end, it was a simple misunderstanding on my part.

Thank you again for the quick response Smile




Theme © iAndrew 2016 - Forum software by © MyBB