Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.4 (DataMapper OverZealous Edition)

[eluser]Mirage[/eluser]
Sorry for the double post in 1.5.3 - I reposting here:

Question: Why doesn't DataMapper automatically try to insert new relations. Right now I have to save related items before I can save the relation itself

Code:
// Each contribution record has ONE transaction
// This works:
$contrib = new Contribution();
$trans    = new Transaction();

$trans->save();
$contrib->save($trans);

// This doesn't work
$contrib = new Contribution();
$trans    = new Transaction();

$contrib->save($trans);

Hope I'm making myself clear. Both objects represent NEW records - I'd have thought that if record has never been saved, DMZ would just go ahead and make it so.

Thanks,
Juergen

[eluser]bEz[/eluser]
@Mirage

would you please post the table schema for each Model....

The DMz manual FAQ section explicitly discuss that unless the related_id are set as "NULL" you will have problems. This sounds like the culprit here.

[eluser]bEz[/eluser]
[quote author="bEz" date="1257918276"]@Mirage

would you please post the table schema for each Model....

The DMz manual FAQ section explicitly discuss that unless the related_id are set as "NULL" you will have problems. This sounds like the culprit here.[/quote]

[eluser]OverZealous[/eluser]
@Mirage
DMZ does not save new related items because there isn't any way for you to check on the success or failure of the save.

Remember, every time you save an object (new or existing), you have to check the result of the save before continuing. If the validation failed, DMZ will not even attempt to save the object.

Basically, it is a good habit to manually save each item. If you find yourself doing something regularly, feel free to override the save() method in your model, save the related item(s), and then save the object itself. At least that way you can decide how to handle errors yourself.

[eluser]mandrake[/eluser]
It's possible generate with Datamapper a query like this:

Code:
SELECT `table`.*, `images`.`*`
FROM (`table`)
LEFT OUTER JOIN `images` as images ON `table`.`id` = `images`.`table_id` AND `images`.`is_main` = 1
ORDER BY `table`.`id` asc
LIMIT 20

Thanks in advance
AT@

[eluser]OverZealous[/eluser]
[quote author="mandrake" date="1258065242"]It's possible generate with Datamapper a query like this:[/quote]

Was that a question? ;-)

If so, you can't make that exact query (there is no way in CodeIgniter's AR to use compound join statements without modifying the CI database libraries).

Depending on what it is you are asking, you might be able to get similar results like this:

Code:
$tables = new Table();
$tables
    ->where_related_image('is_main', 1)
    ->order_by('id', 'ASC')
    ->get();

But you didn't provide enough information. How are image and table related (1-to-N or 1-to-1)? If they are 1-to-1, you can include the columns from images in your results. If they are 1-to-N, there is no way in DMZ, currently, to include the columns. You'll have to run an extra query for each result to get those columns.

The next version of DMZ (yet to be released) will have support for subqueries, which could be used for this. You still won't be able to write compound JOIN statements, and I don't have any plans to include them.

Also, check out the query method, which will let you run your own queries manually. I don't recommend that unless there is no way to build the query otherwise, but I can't see a way around it with your example.

[eluser]bEz[/eluser]
@phil
Ok, I have a unique scenario that hopefully has a configurable solution.

My issue deals with Auto-loading Resources:
Code:
/*
   The following items can be loaded automatically:
   -> Models found in the "models" folder
*/

So, by design, is Datamapper (DMz) library auto-loading "ALL" models or just those which extend "DataMapper" ???

I ask because, I would like somehow setup a model which uses a separate database, and whether it is possible to do so extending "DataMapper" via a config/setting.

[eluser]OverZealous[/eluser]
Every class in the models directory is loaded into memory when DMZ is started. There is no way around this - it is a critical function of DataMapper that it can access any models at startup.

None of these models are instantiated, however, until they are first needed. This means that the database structure of the models isn't loaded until they are first used.

In theory, you should be able to use different DBs for each model. In the next release of DMZ, I have a significant change to the way databases are used, such that every model can specify the database connection information independently. (Of course, there can't be any relationships between models in different databases.)

I hope to have DMZ 1.6 released in the next week or so. I'm busy finishing releasing my big app that I've been building for over a year, though.

[eluser]bEz[/eluser]
Quote:(Of course, there can’t be any relationships between models in different databases.)
See, there you go spoiling my actual concern Sick
O well, back to the drawing board.

[eluser]OverZealous[/eluser]
bEz
Sorry, there just isn't any way to easily support multiple DB queries within DMZ automatically. In many cases, it might not even be possible from within the DB itself.

However, don't overlook the fact that you can manually make the relationship: just query as if the "relationship" column is a normal data column, as opposed to a foreign key. But you won't be able to make one query that has results from multiple databases at all.




Theme © iAndrew 2016 - Forum software by © MyBB