Welcome Guest, Not a member yet? Register   Sign In
First steps of newbie in CI.
#1

(This post was last modified: 10-11-2020, 11:09 AM by jprincon.)

Hello,
I'm just starting to know CI. I need to make a little application for Registration of some users. This users have the posibility of referred new prospects. I need to link that 2 tables. And I want to take advantage of the best of a genial tool like Codeigniter. 
I'm not sure if I can link that 2 tables in the model and if that take me away of the original schema that CI offer to us and miss benefits there.
I like the structure of CI and from the start I want learn to use in the right way.

Sorry for this super basic question, thanks in advance,

JP
Reply
#2

CodeIgniter can do that, you need to use join for joining the tables.

If you show your code here users will be able to show you how to do it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(10-09-2020, 10:05 PM)InsiteFX Wrote: CodeIgniter can do that, you need to use join for joining the tables.

If you show your code here users will be able to show you how to do it.

Thanks InsiteFX, Here I go to try to explain the case (it's very simple):

Here is my controller "Users.php":


Code:
<?php namespace App\Controllers;

use App\Models\ReferredsModel;              /* -->  Here I did include the other Model (ReferredsModel)    */
use App\Models\UsersModel;
use CodeIgniter\Controller;

class Users extends Controller
{
    public function index()
    {
        $model = new UsersModel();
       
        $data = [
            'users'  => $model->getUsers(),
            'title' => 'Lista de Usuarios',
        ];
       
        echo view('templates/header', $data);
        echo view('users/overview', $data);
        echo view('templates/footer', $data);
    }

    public function view($email_user = null)
    {
        $model = new UsersModel();
       
        $data['users'] = $model->getUsers($email_user);
       
        if (empty($data['users']))
        {
            throw new \CodeIgniter\Exceptions\PageNotFoundException('No se logro encontrar: '. $email_user);
        }
       
        $data['email_user'] = $data['users']['email_user'];
       
        echo view('templates/header', $data);
        echo view('users/view', $data);
        echo view('templates/footer', $data);
    }
   
    public function getReferreds($email_referred) /*  -->  Here I did create a function for get data of the other Model */
    {
        $this->load->ReferredsModel();
       
        //$model = new ReferredsModel();
       
        $data['referreds'] = $model->getReferreds($email_referred);
       
        if (empty($data['referreds']))
        {
            throw new \CodeIgniter\Exceptions\PageNotFoundException('No se logro encontrar: '. $email_referred);

        }

        $data['id_referred'] = $data['referreds']['id_referred'];
       
    }

    public function create()
    {
        /* --------------------------------------------------*/
        /*      Here I must to get the referred if exists... *//* --> And here I must to use the data of the other Model*/
        /* --------------------------------------------------*/
        $email_new_user =   $this->request->getPost('email_user');
        $referrred      =   getReferreds($email_new_user);
       
        if (empty($referred))
        {
            $id_ref     =   5;
        }
        else
            {
                $id_ref =   $referred['id_referred'];   
            }

        $model = new UsersModel();
       
        if ($this->request->getMethod() === 'post' && $this->validate([
            'name_user'     => 'required|min_length[3]|max_length[255]',
            'lastname_user' => 'required|min_length[3]|max_length[255]',
            'email_user'    => 'required|min_length[3]|max_length[255]',
            'city_user'     => 'required|min_length[3]|max_length[255]',
            'id_referred'   => 'required',
            'id_profile'    => 'required'
        ]))
        {
            $model->save([
                'name_user'     => $this->request->getPost('name_user'),
                'lastname_user' => $this->request->getPost('lastname_user'),
                'email_user'    => $this->request->getPost('email_user'),
                'phone_user'    => $this->request->getPost('phone_user'),
                'city_user'     => $this->request->getPost('city_user'),
                'payment_method'=> $this->request->getPost('payment_method'),
                'id_referred'   => $id_ref,
                'id_profile'    => $profile,
                'created_date'  => date("Y-m-d h:i:sa"),
                'created_user'  => $this->request->getPost('created_user'),
            ]);
           
            echo view('users/success');
           
        }
        else
        {
            echo view('templates/header', ['title' => 'Crear un nuevo Usuario']);
            echo view('users/create');
            echo view('templates/footer');
        }
    }
   
}


(I'm sorry for this elemental issue. I hope you can understand the point.)

Thanks for all,


JP
Reply
#4

You can not do it like that with QueryBuilder, you need to use the db->query()

With that you can join on two different tables using sql.

You would need two saves one for each id.

You can join on two tables for retrieving the data.

PHP Code:
$db db_connect();

$query $db->query("SELECT * FROM users LIMIT 1;"); 

Use sql to do what you want.

MySQL Tutorial

You can do it with QueryBuilder but you would need to save the first id then switch models.

You did not say if the id's are in different tables but I think they are because your using to models.

Save the first models data then switch tables and save the second models data.

PHP Code:
//$userModel = new \App\Models\UserModel();
save the data

$profileModel 
= new \App\Models\ProfileModel();
//save the data 

That's how you would need to do it with QueryBuilder for getting the data you can use the sql join.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

PHP Code:
$referrred      =   getReferreds($email_new_user); 

This line doesn't do anything (assuming you haven't declared a global scope function called getReferreds.)

You could do it like:

PHP Code:
$referredsModel = new ReferredsModel();
$referred $referredsModel->getReferreds($email_new_user);
$id_ref $referred['referreds']['id_referred'] ?? 5

If that doesn't work, you'd need to show the code for getReferreds method in ReferredsModel.
Reply
#6

He did have a function for that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

Hello again,
I followed the prompts that you kindly wrote to me and I had success driving the 2 models on the same controller.
I preferred to do it like this, using the QueryBuilder and the result was positive.

Thank you very much for your immediate and timely response.

Have a nice day!

JP
Reply




Theme © iAndrew 2016 - Forum software by © MyBB