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

[eluser]MeanStudios[/eluser]
Now for another question regarding turning an object into an array heh.

Now that I'm grabbing those Subscriber Groups, I need to turn the results into an array so I can pass it to a smarty template. I tried passing $sg->all and then use {section} to loop through the results but smarty is outputting it wrong. I'm assigning it like so:
Code:
$this->mysmarty->assign('subscriber_groups', $sg->all);

My smarty code looks like this:
Code:
{section name=i loop=$subscriber_groups}
    &lt;input type="radio" name="subscriber_group_id" value="{$subscriber_groups[i]-&gt;id}" /> {$subscriber_groups[i]->name}<br />
{/section}

And the output is:
Code:
&lt;input name="subscriber_group_id" value="" type="radio"&gt; <br />
&lt;input name="subscriber_group_id" value="1" type="radio"&gt; Test Subscriber Group<br />
&lt;input name="subscriber_group_id" value="2" type="radio"&gt; Another Test Group<br />

It should be outputting 3 records:
ID: 1 - Name: Test Subscriber Group
ID: 2 - Name: Another Test Group
ID: 3 - Name: Third Test Group

As you can see it is outputting 3 radio buttons but the first one is blank. This might be a problem with smarty? That's why I decided to convert the object into an array and then pass it to the smarty template that way. I tried passing $sg->_to_array() but that only takes the first record and turns it into an array, which is not what I wanted. Is there another hidden function that will turn the object values into an associative array? Or should I do something like:
Code:
$subscriber_groups = array();
foreach ($sg->all as $subscriber_group)
{
  $subscriber_groups[] = $subscriber_group->_to_array();
}

Would that be the more efficient way of doing it?

[eluser]Daniel H[/eluser]
I have a problem where DMZ isn't recognising that the relationship field is on the table itself, and so throws a wobbly when it can't find a join table.

I have two tables, countries and states, where each country can have multiple states.

My states table and model:

Code:
CREATE TABLE `states` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `country_id` int(10) unsigned default NULL,
  `name_long` varchar(20) NOT NULL COMMENT 'Common Name',
  `name_short` char(2) NOT NULL COMMENT 'USPS Abbreviation',
  PRIMARY KEY  (`id`)
)

Code:
class State extends DataMapper {

    var $has_one = array('country');
    
    var $has_many = array('directory_entry');

    function __construct()
    {
        parent::DataMapper();
    }
    
}


My countries table and model:

Code:
CREATE TABLE `countries` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `iso` char(2) NOT NULL,
  `name` varchar(80) NOT NULL,
  `iso3` char(3) default NULL,
  `numcode` smallint(6) default NULL,
  `featured` tinyint(1) default '0',
  PRIMARY KEY  (`id`)
)

Code:
class Country extends DataMapper {

    var $has_many = array('directory_entry', 'state');
    
    function __construct()
    {
        parent::DataMapper();
    }
}

So when I perform the following query:

Code:
$states = new State();
$states->include_related('country', array('name'));
$states->order_by('name_long', 'asc');
$states->get();

I get a db error telling me that Table 'db.join_countries_states' doesn't exist (I have join_ set as a prefix in my config - removing this makes no difference). The country_id field is clearly on the state table and as far as I can see, the relationships are correct?

I am using version 1.3.3 (same happened with 1.3.2).

[eluser]MeanStudios[/eluser]
In your Country model I would try setting the table and model globals just in case it's getting the pluralization wrong.
Code:
var $table = 'countries'
var $model = 'country'
Couldn't hurt, might help.

[eluser]Daniel H[/eluser]
Yeah I tried that but it wasn't the issue.

I did some debugging and it just seems that $this->fields is returning an empty array when it gets to _get_relationship_table() such that if (in_array($other_model . '_id', $this->fields)) will always return FALSE since the array is empty. I'm not sure why...

[eluser]OverZealous[/eluser]
@Daniel H
Your problem is the $fields array being empty. It is filled in the DataMapper constructor. There are only three reasons I can see for this (off the top of my head, since I am not able to see the source):

1) You have a different class within your CodeIgniter app called Country. For example, it might be a controller, a library, or within a helper.

2) Somehow, the Production Cache has been enabled. See the DMZ docs for how to disable this.

3) Your class that you are working with has an incorrectly named table or model, and that is causing the problem.

[eluser]Ivan Augusto[/eluser]
Hello... I'm having problems with making a relationship.

My Tables:

Code:
TB_CLIENT
id

TB_CONTACT
id
client_id

1 Client : N Contacts
1 Contact : 1 Client

I'm trying to do this:

Code:
class Client extends DataMapper {
    
    var $table = "client";
    var $has_many = array(
        'contact' => array(
            'class' => 'contact',
            'other_field' => 'client_id'
        )
    );

    function __contruct() {
        parent::__construct();    
    }
}

class Contact extends DataMapper {
    
    var $table = "contact";
    var $has_one = array(
        'client' => array(
            'class' => 'client',
            'other_field' => 'id'
        )
    );
    function __contruct() {
        parent::__construct();    
    }
}

and in Controller:

Code:
$client = new Client();
$contact = new Contact();

if ($client->save($contact)) {
    echo 'Save';
} else {
    echo 'Error';
}

But always gives error! Someone help me please!
xD~

Thks a lot =D
[]s

ps.: sorry my bad english

[eluser]OverZealous[/eluser]
[quote author="MeanStudios" date="1247026315"]Is there another hidden function that will turn the object values into an associative array? Or should I do something like:
Code:
$subscriber_groups = array();
foreach ($sg->all as $subscriber_group)
{
  $subscriber_groups[] = $subscriber_group->_to_array();
}

Would that be the more efficient way of doing it?[/quote]

Sorry about the delay on replying to this. Currently, there is no hidden method. I would use the example you provided, but wrap it in an Extension, so you can easily use it on any model. I recommend calling it all_to_array. If you decide to do this, then it can be shared.

I might add it as an extension myself, if I get some time.

[eluser]OverZealous[/eluser]
@ivan Augusto

It looks like you are just setting up a normal relationship. You don't need to specify any of the advanced stuff you listed, instead just do this:
Code:
class Client extends DataMapper {
    
    // Don't specify $table.  Instead, name your database table 'clients' (or 'tb_clients' if you use the prefix 'tb')

    var $has_many = array(
        'contact'
    );

    // rest of class
}

class Contact extends DataMapper {
    
    // Don't specify $table.  Instead, name your database table 'contacts' (or 'tb_contacts' if you use the prefix 'tb')

    var $has_one = array(
        'client'
    );

    // rest of class
}

DMZ will automatically use the in-table client_id on the table contacts. You must name your tables with lowercase names. If you want to use the tb prefix, make sure you specify it in your config/datamapper.php class.

Please take some time to read through the extensive documentation, because most of this is explained fairly well.

[eluser]MeanStudios[/eluser]
[quote author="OverZealous.com" date="1247215056"][quote author="MeanStudios" date="1247026315"]Is there another hidden function that will turn the object values into an associative array? Or should I do something like:
Code:
$subscriber_groups = array();
foreach ($sg->all as $subscriber_group)
{
  $subscriber_groups[] = $subscriber_group->_to_array();
}

Would that be the more efficient way of doing it?[/quote]

Sorry about the delay on replying to this. Currently, there is no hidden method. I would use the example you provided, but wrap it in an Extension, so you can easily use it on any model. I recommend calling it all_to_array. If you decide to do this, then it can be shared.

I might add it as an extension myself, if I get some time.[/quote]
That is exactly what I did Smile - all is good Wink.

[eluser]mcnux[/eluser]
Hi all, I'm having trouble deleting a "has_many" relationship.

Here's the database tables (I've removed some irrelevant columns for the sake of clarity):

User Table
Code:
CREATE TABLE users(
  id int(10) unsigned NOT NULL auto_increment,
  displayName varchar(45) NOT NULL,
  PRIMARY KEY  (id)
);

Client Table
Code:
CREATE TABLE clients (
  id int(10) unsigned NOT NULL auto_increment,
  user_id int(10) unsigned default NULL,
  companyName varchar(100) NOT NULL,
  PRIMARY KEY (id),
  KEY fk_client_user (user_id),
  CONSTRAINT fk_client_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION
);

As you can see, the clients table has a foreign key for it's user but when I try to delete the relationship by doing the following:

Code:
$user = new User();
$user->get_by_id($id);
$user->delete($user->clients->get()->all);

The foreign key (user_id) is not being set to NULL on the client records as I was expecting. I have verified that the relationship is setup correctly in the model; $user->clients->get()->all does return me the client records as expected.

I have a feeling the problem may lie in the fact that the relationship is setup using a foreign key and not a join table (as the original DataMapper expected). Someone please tell me this isn't the case, I've looked at the OverZealous source and would rather not have to mess around with it to get this to work.

Great job with this mapper by the way, I'm sure I'm going to really appreciate it once I've gotten around my teething problems.

UPDATE:
I have found that the relationship is removed if I delete the user, so the mapping is working as expected but I am still unable to delete just the relationship (and not the user) which I need to do - I'm trying to reassign a user's clients to a different user so ideally what I want to do is:
Code:
$userOne = new User();
$userOne->get_by_id($id);

$userTwo = new User();
$userTwo->save($userOne->client->get()->all);




Theme © iAndrew 2016 - Forum software by © MyBB