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

[eluser]12vunion[/eluser]
[quote author="ennis" date="1256746546"]
The client->relatedclient works great but now I have a field in my clients_relatedclient table that is called status and per default i only want the clients out who are "active".

How would i do this with out adding a custom method to my Clients class?

Thanks.[/quote]

Get (Advanced)
Code:
$client->relatedclient->where_join_field($client, 'status', 'active')->get();

[eluser]ennis[/eluser]
Doesnt work says that stauts is an unknown column

[eluser]OverZealous[/eluser]
[quote author="ennis" date="1256912347"]Doesnt work says that stauts is an unknown column[/quote]

Please at least post some useful information.

Have a look here Troubleshooting - How can I see the queries being generated? and look at the queries being generated.

Without at least that, there's no way anyone can help you. You might be able to work through what's wrong on your own.

[eluser]Benedikt[/eluser]
I have a User-model which has the following method and a Country-Model which representes the Users Country.

Code:
public function register () {
  $u = new User();
  $c = new Country();
  $c->get_by_id(3);

  $u->username = 'Fred'.
  $u->save( $c );
}

The User "has_one" Country, the Country "has_one" User. Now, in the controller I use $u = new User(); which ends up in an endless loop creating Country-Objects.

I saw in the example application you do sth similar. But what is wrong in my case? Can I not create a model inside a model?

Thanks for your help.

[eluser]OverZealous[/eluser]
@Benedikt

Your problem is most certainly outside your example code. There is no reason why creating a model inside another model should cause a loop.

You might try disabling auto_populate_has_one, but that is only used the first time you access the child item, so it shouldn't be the case.

Do you have some validation routines running on save that could be causing it?

[eluser]Benedikt[/eluser]
@OverZealous: Thanks for your reply. No, there is validation running.
I use a creator like this:

Code:
public function User () {
  parent::DataMapper();
  /* Logging */
  log_message('debug', 'User object created.');
}

Is this a problem?

[eluser]OverZealous[/eluser]
Post the code you are using to create the country objects. That's what you said was causing the issue! Obviously if your constructor doesn't do anything, it isn't going to cause a problem.

[eluser]Benedikt[/eluser]
Code:
class Country extends DataMapper {

    var $table = 'countries';
    var $has_many = array("user", "club");

    public function Country () {
      parent::DataMapper();

      /* Logging */
      log_message('debug', 'Country object created.');
    }
  }
Hm, it seems that the User-Creator is not called at all. At least the log "User object created." wont show up.

[eluser]Alface[/eluser]
I'm having a big problem here, I just can't find a way to auto validate the relational object of Videosalbum, Video object.. it just don't show me any erros, I think thats because I'm using $va->trans_status() than $va->save() as well..

Anyone can help me?
Controller:
Code:
function videosalbum($id = NULL, $apagar = NULL){
        $va = new Videosalbum($id);
        $v = new Video();
        if($apagar=='apagar' && $id != NULL){
            $va->delete();
            redirect(__CLASS__.'/listar/'.__FUNCTION__);
        }

        if($_POST){
            $va->from_array($_POST);

            $va->trans_start();
            foreach($this->input->post('videos') as $vId => $video){
                if($vId == 'novos'){
                    foreach($video as $novos){
                        $v->from_array($novos);
                        if($v->validate()){
                            // Se o campo estiver preenchido então salva
                            $v->id = NULL;
                            $v->save();
                            $va->save($v);
                        }
                            
                    }
                }else{
                    $v->from_array($video);
                    $v->id = $vId;
                    if($v->url){
                        // Se o campo estiver preenchido então salva
                        $v->save();
                        $va->save($v);
                    }else{
                        // Se o campo estiver em branco então apaga
                        $v->delete();
                    }
                }
                
            }
            if ($va->trans_status() === FALSE){
                $va->trans_rollback();
            }else{
                $va->trans_commit();
                echo '<p>Valores:</p>';
                echo '<code><strong>ID</strong>: ' . $va->id . '<br />' .
                '<strong>nome</strong>: ' . $va->nome . '</code>';
                //redirect(__CLASS__.'/listar/'.__FUNCTION__);
            }
        }

        $data['data']['campos'] = array('Informa&ccedil;&otilde;es do Album de video' => 'section',
                                        'nome',
                                        'videos'
                                    );
        $data['data']['url'] = __CLASS__.'/'.__FUNCTION__.(empty($object->id))?'':'/'.$object->id;
        $data['content'] = __CLASS__.'/'.__FUNCTION__;
        $data['data']['va'] = $va;
        $this->load->view('layout', $data);
    }

[eluser]OverZealous[/eluser]
@Alface
Edit: removed rude comment - sorry, I just got up.

Validation is automatically run when you call save. This has nothing to do with the transaction status. Every time you call save(), you must check the result, for every object.

If validation fails, the save will not go through. The whole point of using transactions is that you can safely save multiple items, and if the last one fails, the DB will rollback all of the changes. In fact, you have to do it this way, because DMZ expects "existing" items to exist in the database, even ones just saved.

Finally, the transaction status is only useful in explaining when the database itself has been unable to save. If the failure was in the validation of your DMZ model, the transaction status will be TRUE, because no save query was processed.

So, again, your code needs to look like this:
Code:
if($object->save()) {
    if($object2->save($object1)) {
        // etc
    } else {
        // etc
    }
} else {
    // fail
}

or like this
Code:
$success = $object->save();
$success = $success && $object2->save();

if(!$success) {
    // process $object->error and $object2->error
}




Theme © iAndrew 2016 - Forum software by © MyBB