0

I have 2 tables Users and Artists.

User:
id
name
password
city_id
state_id
country_id

Artist:
id
user_id
description

I wish to create add and edit form(single form acts as add and edit). But on click of edit link, I wish to fill in the form fields from user as well as artist table. How do i do that? Below is my controller code:

public function artist_register() {
        $this->country_list();
        if ($this->request->is('post')) {
            $this->request->data['User']['group_id'] = 2;
            if ($this->{$this->modelClass}->saveAll($this->request->data)) {
                $this->redirect(array('controller' => 'Users', 'action' => 'login'));
            }
        }
        $this->render('artist_update');
    }

    public function artist_update($id) {
        $artist = $this->{$this->modelClass}->findByuser_id($id);
        $artist_id = $artist[$this->modelClass]['id'];

        if($this->request->is('get')) {
            $this->request->data = $this->{$this->modelClass}->read(null, $artist_id);
            $this->request->data = $this->{$this->modelClass}->User->read(null, $id);
        } else {
            if($this->{$this->modelClass}->save($this->request->data)) {
                $this->Session->setFlash('Your Profile has been updated');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

The code doesnt work. how do i solve it?

tereško
  • 58,060
  • 25
  • 98
  • 150
z22
  • 10,013
  • 17
  • 70
  • 126

1 Answers1

1

First thing to do:

You have to define a relationship between User and Artist.

A user has one artist
A user has many artist
A artist belongs to an user

In this case, I think the relationship you're looking for is:

A artist belongsTo user

Time to code:

Knowing this, you can now define the relationship. In your Artist model you need to declare the belongsTo variable:

class Artist extends AppModel {
    public $belongsTo = 'User';
}

Or you can setup the relationship like this:

class Artist extends AppModel {
    public $belongsTo = array(
        'User' => array(
            'className'    => 'User',
            'foreignKey'   => 'user_id'
        )
    );
}

For your information:

There is another possible keys to use in the belongsTo association:

  • className: the classname of the model being associated to the current model. If you’re defining a ‘Artist belongsTo User’ relationship, the className key should equal ‘User.’

  • foreignKey: the name of the foreign key found in the current model. This is especially handy if you need to define multiple belongsTo relationships. The default value for this key is the underscored, singular name of the other model, suffixed with _id.

  • conditions: an array of find() compatible conditions or SQL strings such as array('User.active' => true)

  • type: the type of the join to use in the SQL query, default is LEFT which may not fit your needs in all situations, INNER may be helpful when you want everything from your main and associated models or nothing at all! (effective when used with some conditions of course). (NB: type value is in lower case - i.e. left, inner)

  • fields: A list of fields to be retrieved when the associated model data is fetched. Returns all fields by default.

  • order: an array of find() compatible order clauses or SQL strings such as array('User.username' => 'ASC')

What to expect?

This is the results from a $this->Artist->find() call after defining our relationship.

Array
(
    [Artist] => Array
    (
        [id] => 5
        [user_id] => 1
        [description] => I am an artist!
    )
    [User] => Array
    (
        [id] => 1
        [name] => Fuhrmann
        [passsword] => 1234
        [city_id] => 500
        [state_id] => 2020
        [country_id] => 223
    )

)

So what?

After all this, you are able to access the User when looking for a Artist:

public function artist_update($id) {
    // Read the artist and the user info
    $artist = $this->{$this->modelClass}->findByUserId($id);        
    $this->set(compact('artist'));
}

And in your view, you can get the name of the user or another field in the Users table:

<input type="text" name="name" value="<?php echo $artist['User']['name'];?>"
<input type="text" name="user_id" value="<?php echo $artist['User']['id'];?>"
<input type="text" name="country_id" value="<?php echo $artist['User']['country_id'];?>"

Read more

Read more about Associations: Linking Models Together

Fuhrmann
  • 557
  • 13
  • 35