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

[eluser]OverZealous[/eluser]
mcnux

There are two solutions to your problem.

1) Since you are remapping the client to a different user, the easiest solution is to simply save the clients to the new user. Because each client can only be related to one user (both by rule AND by database design), the new $userTwo->id will be set in $client->user_id. You will still need to load the new Clients on the new User ($userTwo->client->get()) to see the update.

Also, the old user will still have the related set in memory for this HTTP request (meaning, $userOne->client->all will still contain a reference). There is no way for DM to know that you are moving clients from one user to another, due to PHP garbage collection limitations.

2) $userOne->delete($client) requires that $client->user_id be able to be stored as NULL. When you set FK rules in most databases, you often need to explicitly allow NULLs, as is mentioned in the docs (under Rules). Please read over your database's docs, and make sure that your rules are not preventing the column from being set as NULL.

[eluser]80onelove[/eluser]
Here's one I've been mulling over for a few days:

I'm working on a group blogging platform. I have a table for users and a table for blogs, which are joined by table blogs_users.

Users has many Blogs
Blogs has many Users

Now, I'd like to grant different privileges to different users on different blogs. Here's an example:

User_1 is joined to Blog_1 and Blog_2, but is granted Level_2 privilege for Blog_1 and Level_4 privilege for Blog_2.

Can this be achieved using DMZ? Or can it be accomplished merely using DataMapper? I've been toying around with this for a while, and the only solution I can come up with is having a privilege table with columns for user, blog, and privilege, but I'm wondering if this is redundant?

[eluser]OverZealous[/eluser]
@80onelove

It's possible, but not really practical, with the original DataMapper (it requires using a dedicated model to handle the relationships, and a lot of extra queries to look them up)

With DMZ, you can easily use join fields to add one or more additional columns on blogs_users. The column could be as simple as storing a value for the access level, or the ID of a privilege model. Or you could store more complex values on the join table directly.

Then you can query using that value, and include it when getting results.

[eluser]80onelove[/eluser]
@overzealous

Yep, I'm now using DMZ joins and it's working like a charm... much improved over the existing DM mapping functions. Cheers!

[eluser]Daniel H[/eluser]
Okay I figured out my country and state problem. Basically the field_data function doesn't play nicely with db caching, which I hadn't disabled. So basically every time I need to do an 'include_related' or similar, I need to turn the db cache off and then turn it back on again.

Essentially field_data returns an empty array if the cache is enabled, since the data cannot be cached. Quite frustrating!

[eluser]OverZealous[/eluser]
For anyone wondering, this is due to the way CodeIgniter's query caching works, and is completely unrelated to DataMapper. The CI guide even explains the risks of using query caching.

I actually have an extension in the pipeline (it's already built) that will allow you to easily use the above query caching without worries. (Basically, to use a cacheable query, you'd call get_cached instead of get.)

That, along with some improvements and extensions, are waiting until I can build a dedicated test / example website. That is actually my plan for today.

[eluser]Daniel H[/eluser]
Sounds superb - will look forward to seeing it!

[eluser]mrtavo[/eluser]
Hello every one,

First of all, congratulations for this DMZ plugin, it's very impressive; and in second place, forgive my bad English.

I've tried to do a simple relationship 1:N between 2 objects, let say, object A have 1 object B and object B have N relations with A. The point is that I'm using sqlite database.

So, the configuration rules must be something like this:

ObjectB:

Code:
class B extends DataMapper {
  var $app;
        
  var $has_many = array(
    'AR' => array(
      'class' => 'A',
      'other_field' => 'BR'
    )
  );
        
  var $validation = array(
    'name' => array(
      'label' => 'Name',
      'rules' => array('required', 'min_lenght'=>4)
    ),
    'description' => array(
      'label' => 'Description',
      'rules' => array()
    )
  );

  var $table = "Bs";

/**
* Constructor
*/
  function B(){
    $this->app = & get_instance();
    if (!($this->app->db->table_exists('Bs'))){
      echo ('Create the table</br>');
      $this->_create_Bs_table(); /* This creates a sqlite table if it doesn't exists */
    }
    parent::DataMapper();
  }
}

Object A:
Code:
class A extends DataMapper {
    var $app;

    var $has_one = array(
      'BR' => array(
        'class' => 'B',
        'other_field' => 'AR'
      )
    );
    var $table = "As";    
        
    function A(){
      $this->app = & get_instance();
      if (!($this->app->db->table_exists('As'))){
        $this->_create_As_table();
      }
      parent::Datamapper();
    }
}

To try all this i have code a simple example:
Code:
function cr(){
    $Bs = array(
      new B(),
      new B(),
      new B()
    );
    $Bs[0]->name = "XX";
    $Bs[0]->description = "XX";
    $Bs[1]->name = "YY";
    $Bs[1]->description = "YY";  
    $Bs[2]->name = "ZZ";
    $Bs[2]->description = "ZZ";
    $Bs[0]->save();
    $Bs[1]->save();
    $Bs[2]->save();

    for ($j = 0; $j < count($Bs); $j++){
      for($i = 0; $i < 10; $i++){
        $E = new A();
        /* Init A here */
        $E->save($Bs[$j]);
      }
    }
    echo ('</br></br>');
    $Bs[0]->AR->get();
    foreach( $Bs[0]->AR->all as $Ao ){
      echo ($Ao->name."</br>");
    }
}

This works fine with mysql, but not with sqlite. With sqlite i get this error:
(i need to use sqlite, for some reasons)

A Database Error Occurred

Error Number: 1

SQL logic error or missing database

SELECT As.* FROM (As) LEFT JOIN Bs as Bs ON Bs.id = As.B_id WHERE Bs.id = 10



I suppose it's something i have done wrong, but i don't know what i missed; because the same code (except table creation method) works with mysql.
In fact, the relations are done, because in the sqlite tables they are well inserted, (I can see them using scaffolding), and give no errors.

Any ideas?

* sqlite database exists and if it doesn't, it's created with a pre_system hook.
* sqlite tables exists, i have done some querys this db object.
* sqlite relations exists, and it's possible to do standard querys to the db object and it gives the real (and well formed) result. (but not with LEFT JOIN)
* it seems to be a sqlite syntax problem. (maybe)

[eluser]OverZealous[/eluser]
Original comment removed, see below.

[eluser]OverZealous[/eluser]
Man, I gotta stop writing before I wake up. See below.




Theme © iAndrew 2016 - Forum software by © MyBB