Jump to content

Tank Auth In Code Igniter - How To Get The User's Id In Any Other Controller?


Recommended Posts

::)

 

I know how Sessions and Cookies work, thanks.

 

The solution was to create a Controller that extends CI_Controller, and load up the tank_auth in that controller. Then make my other controllers extend that new controller instead of CI_Controller.

Ah, now I see what you are asking.

 

I always extend CI_Controller and CI_Model with my own new base classes and everything else extends off those.

 

Does Tank give you acl support or something? Why did defer auth to this lib?

This is my first time trying to do a project with CI - and of course I started the day their site was down ::) Fortunately their user guides were hosted elsewhere for the day.

 

I chose that lib because when I googled "User Authentication for Code Igniter" it got the most hits ;)

 

I'm still learning how exactly CI works. I am not 100% sure this is actually saving me time with my project :/ 

I see. FYI docs come with your download/clone, unless you blew them away. The CI docs are very well organized and I find them absolutely a delight to work with. Well if this is your first rodeo with CI here are some tips:

 

1) your My_Controller and MY_Model classes are absolutely priceless. I use the same in every project I create.

2) file structure is rather important, for organization and security. I wouldn't use the suggested structure. Here is mine:

 

/

/application

/system

/public_html (doc_root)

 

This keeps all MVC's outside of doc_root. The only thing sitting in public_html is essentially a single index.php file, css, img and js dirs.

 

3. htaccess is your friend!

4. turn off all debugging and error reporting for php and db stuff when going live

I'm having trouble figuring out how to get models or controllers to interact with one another. In the frameworks I'm used to each model represents a table. It seems like CI suggests the same.

 

So here are some tables I have:

users

quests

users_quests

 

users_quests contains a PK, a FK to users and an FK to quests (and some DATETIMEs). 

 

I'm in my Quests Controller. index.php/quests/view/5 (5 = pk on users_quests)

 

What is the smart way to pull all of the users_quests data and the related quests_data.

 

I know how to do it in pure SQL, and I know how to do it in Yii - I don't get how to do it in CI, automatically anyway. I can write a chained DB statement: pseudo code as 

$this->db->select('fields')->from('users_quests')->join('quests', 'columns')->where('col=5');

 

That seems like a ton of stupid code for a simple BELONGS TO.

Edited by Jessica

I agree model/table relationship. However I occasionally have models that don't have tables.

 

So how I would do this or actually how I do do this would be like this:

 

quest controller:

$this->load->model('quest');

 

$options = array('quest_id' =>$this->uri->segment(3)->, 'limit' => '1');

$quest = $this->quest->retrieve($options);

 

quest model:

this is where all the heavy lifting is done, the idea here is FAT models and SKINNY controllers

Lots of code in the model, this is where having a solid MY_Model comes in to play.

 

I have a more detailed answer but I am running late and have to go, I will post some more later.

I'm having trouble figuring out how to get models or controllers to interact with one another. In the frameworks I'm used to each model represents a table. It seems like CI suggests the same.

I imagine this is because you're used to ORM. This isn't so much the case with CI, and I bet $20 you'll really like their db driver.

// Random Controller
$this->load->model('poll_model', 'Polls');
$Poll = $this->Polls->get_poll(array('id'=>3));

// Poll_model
function get_poll($data = array())
{    
 // Check for valid data
 if(empty($data) OR !is_associative($data))
 {
   // Invalid data, return FALSE
   return FALSE;
 }

 // Retrieve the query from the database
 $query = $this->db->where($data)->get('polls', 1);

 // Check if query row exists
 if($query->row())
 {
   // Query row exists, return query row
   return $query->row();
 }
 else
 {
   // Query row doesn't exist, return FALSE
   return FALSE;
 }
}

 

So here are some tables I have:

users

quests

users_quests

 

users_quests contains a PK, a FK to users and an FK to quests (and some DATETIMEs). 

 

I'm in my Quests Controller. index.php/quests/view/5 (5 = pk on users_quests)

 

What is the smart way to pull all of the users_quests data and the related quests_data.

 

I know how to do it in pure SQL, and I know how to do it in Yii - I don't get how to do it in CI, automatically anyway. I can write a chained DB statement: pseudo code as 

$this->db->select('fields')->from('users_quests')->join('quests', 'columns')->where('col=5');

 

That seems like a ton of stupid code for a simple BELONGS TO.

 

Joins are always cumbersome, and no different with CI. I would write this:

// Controller
$pk = $this->uri_segment(3);
$this->load->model('quest_model', 'Quests');
$Data = $this->Quests->name($pk);

// Model
function name($id) {
 $this->db->select('fields')->from('users_quests')->join('quests', 'quests.user_id = users_quests.user_id')->where('users.user_id = ' . $id)->get();
}

Edited by Mahngiel

That's what I have now.

 

In the framework I've used before, the model will have an array of relations, and you say this model is a many-to-many or a belongs to. Then you just access it as $model->relatedmodel. And it's automagic.

 

Why aren't I using that framework? Who knows. I like to make things harder on myself I guess.

 

Edit: I'm not trying to hate on CI, I just am having trouble understanding what effort this is saving me. Why is this better than writing the SQL myself? I get using a framework that actually generates the joins and everything, but...

 

SELECT fields FROM table LEFT JOIN table_2 on table_2.pk = table.fk WHERE pk = 1

or

$this->db->select('fields')->from('users_quests')->join('quests', 'quests.user_id = users_quests.user_id')->where('users.user_id = ' . $id)->get();

or:

$model->relation['table2'];

$model->table2;

 

I don't see why CI's way is helping me write code faster, and automate tasks. That's the point of a framework?

Edited by Jessica

There use to be a ORM for CI called Doctrine, you might want to check into that. ORM conversations, whether to use one or not, can get crazy. Like discussing what is the best editor/IDE is between coders. I will never understand those heated editor flame wars, when everybody knows vim is the best :) ... i guess they are arguing over 2nd place.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.