Codeigniter HMVC + ion_auth trouble loading the config items - codeigniter

I have been banging my head for 5 hours and I finally solved the problem but I just cannot go to sleep without knowing the reason. Let me explain the issue first.
I have used codeigniter HMVC extension and installed ion_auth as a separate module.
|-modules
|--auth
|---config
|-----ion_auth.php
|---controllers
|-----auth.php
|---models
|-----ion_auth_model.php
|---views
When I was trying to get a user's group I started to get wired SQL errors. Then I narrowed the issue and figured out that the items in config/ion_auth.php were not loaded in the ion_auth_model.php file.
ERROR - 2016-02-24 20:09:26 --> Query error: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'as id, .name,
.description JOIN ON .=.id WHERE . = '2'' at line 1 - Invalid
query: SELECT . as id, .name, .description JOIN ON .=.id
WHERE . = '2'
Then I tried couple of stuffs and when I remove the index 'ion_auth' from
couple of method calls in ion_auth_model.php everything started to work.
I changed
$this->tables = $this->config->item('tables', 'ion_auth');
$this->join = $this->config->item('join', 'ion_auth);
to
$this->tables = $this->config->item('tables');
$this->join = $this->config->item('join');
Can anyone tell me why it worked?

This is the inner implementation of the CodeIgniter function config->item() found in the file system\core\Config.php
/**
* Fetch a config file item
*
* #param string $item Config item name
* #param string $index Index name
* #return string|null The configuration item or NULL if the item doesn't exist
*/
public function item($item, $index = '')
{
if ($index == '')
{
return isset($this->config[$item]) ? $this->config[$item] : NULL;
}
return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
}
When you pass the $index parameter, the function checks if both parameters are initialized in the config and returns config[$index] of the CI instance; or null if any of them is not initialized.
If config[$item] is not set in the CI instance, the function returns always null. Let's assume this is not the case, as your call don't crash when avoiding $index.
So when you pass $index as the second parameter, your code crashes because the function returns null, and that means that the config[$index] of the CI instance is not set. Now the question is why it's not set, and I can't help you here, but it looks like you are missing to load some modules.
Best regards

Related

How do I create a new Eloquent model with an incrementing field if the name exists?

Hello i have a schedule table in my database, users are allowed to save their schedules into the database, my problem is a situation where for some reason the user has Two schedules with the same name on the same date. I use the fullCalendar plugin to capture my info from the user. Also records are deleted by title, start, and end dates.
I wanted to know if it was possible in eloquent to let the system check for the existence of a particular record and add a number to the title whensaving the record if not found then it would just save the title without the number.
Is this possible? Is there already a feature like this in eloquent? If yes how can it be done.
Note: I know i can do this in my controller by checking for existence and adding a number to the retrieved title my question is if there is already a way to do this directly from the model without having to write code in the controller like how slugs are generated.
Well since i was unable to find a solution i just wrote this function instead:
/**
* #param $str
* #param Model $model
* #param $column
* #return string
*/
protected function increment_if_exists($str, \Illuminate\Database\Eloquent\Model $model, $column)
{
// Finding max ID of any occurrence of specified string
$max = $model::select($column)->whereRaw($column ." REGEXP '" . addSlashes($str) . "'")
->orWhereRaw($column ." REGEXP '" . addSlashes($str) . " \\\\([0-9]+\\\\)'")
->max('id');
// If 0 matches found supplied string is used.
// If 1 or more matches found 1 is added to the returned value and appended to supplied string in parenthesis.
$new = empty($max) ? $str : $str . ' (' . ($max + 1) . ')';
return $new;
}
I use it in my controller so it's not really exactly what i wanted but perhaps somebody will see this and come up with a way of integrating this into a model so that it does it for you automatically on save.

Laravel Eloquent sql statement after exist check is limited to 1

I've found a really weird behaviour in the current laravel 4.2 patch. Until now whenever I want to get some datas from my eloquent model I check if results are available before i get them via the following code example:
$foo = $model->hasManyRelationFunction();
if($foo->exists())
foreach($foo->get() as $elem)
....
this results in the following sql statement:
select count(*) as aggregate from "vendor_tabs" where "vendor_tabs"."vendor_id" = '80' limit 1 <-- exist() ?
following by the real select sql statement without Limit 1, for example:
select * from "vendor_tabs" where "vendor_tabs"."vendor_id" = '80' order by "id" asc
Since i've updated laravel 4.2 to the current patch, it also limits the real sql statement to 1
`select * from "vendor_tabs" where "vendor_tabs"."vendor_id" = '80' order by "id" asc` limit 1
but ONLY when I make the if($foo->exists()) check. As soon i comment out the exist condition everything works fine. Are their any informations about this behaviour or am I just stupid? :D
Edit: It seems like they made the eloquent builder "more fluent" in patch 4.2.13. I still dont know if this is a bug or a feature, imo this shouldnt be the normal behaviour.
/**
* Determine if any rows exist for the current query.
*
* #return bool
*/
public function exists()
{
$limit = $this->limit;
$result = $this->limit(1)->count() > 0;
$this->limit($limit);
return $result;
}
In fact you don't need to check existence. You should only do:
foreach ($model->hasManyRelationFunction as $elem) {
// do whathever you want
}
and it's just enough to get the data. You don't need to check existence here or if you think you do, you should show a real example of a code what you are trying to achieve.

laravel 4 Illegal offset type in isset or empty

I am using laravel 4 and trying to perform store of some data. I am having a problem that says:
Illegal offset type in isset or empty
* Get the fully qualified location of the view.
*
* #param string $name
* #return string
*/
public function find($name)
{
if (isset($this->views[$name])) return $this->views[$name];
if (strpos($name, '::') !== false)
I can't understand where is this coming from. Anyone that can help me?
Looks like the $name variable is undefined, are you sure you are setting it?
the type of $name is something that is not a string or an integer
check the var type in newer PHP (5.3>) this is more strict
probably you are sending on $name an object or an array and don't realize
I could reproduce it sending an array
ie
$name = array("test" => "testvalue");
$obj->arr[$name]; //this line give me an error because $name is an array
best
Emiliano

Zend DbTable case insensitive

I have a login system for my webapp that works well using the Zend auth adapter but the problem is I want the email to be case insensitive when a user logs in. I am using Oracle as the back end DB and normally I would user the LOWER(EMAIL)=LOWER(:email) method. I tried to pass that Oracle function in the setIdentityColumn() but I get the error:
The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce
a valid sql statement, please check table and column names for
validity.
protected function _getAuthAdapter()
{
//$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$db = Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn('LOWER(EMAIL)') //Tried to pass LOWER()
->setCredentialColumn('ENCODED_PW')
->setCredentialColumn('PASSWORD');
return $authAdapter;
}
The error is coming from the function _authenticateCreateSelect() in the Zend_Auth_Adapter_DbTable class. The problem is this part of the script:
$dbSelect->from($this->_tableName, array('*', $credentialExpression))
->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
The quoteIdentifier() method is like PHP quote() and is turning a query like this:
select * from LOWER(:email)
into this:
select * from "LOWER(:email)"
Anyone see a way around this?
Kind Regards
Nathan
Try something like this:
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn(new Zend_Db_Expr('LOWER(USERID)'))
->setCredentialColumn('PASSWORD');
The problem is that if you pass 'LOWER(USERID)' as a simple string, Zend will put quotes around it, causing it to create an invalid query. Using Zend_Db_Expr will stop Zend doing this.

Laravel 4: how can I understand how it all works?

I am using Laravel 3 in one project and it's been a joy. I have also looked at the source code several times to see how some things work behind the scenes.
But now in Laravel 4, I don't know where to begin or how to understand it all. Where can I learn all the behind the scenes of Laravel 4?
Case in point: I wanted to find out if the DB::insert() returns the id of inserted row. So I started searching.
1. I found the Illuminate\Support\Facades\Facade class that "encapsulates" DB.
2. The resolveFacadeInstance function is called and then I tried to print these arrays, but my computer hangs :-/. And I'm sure this would lead to many more classes that I wouldn't understand.
Is there a way I could try to learn the inner workings of Laravel 4? Maybe stack traces?
The facade class is just a filter class to allow you to call methods as if they were static.
For the facade mappings go here: http://laravel.com/docs/facades#facade-class-reference
The starting point to fully understand laravel's inner-workings should begin at:
/public/index.php
You can follow the logic of the program, noticing that requires start.php, which loads an instance of the "Application" which is found here:
/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
This Tuts+ video shows a couple of ways of finding out what class is actually doing the work.
E.g.:
$root = get_class(DB::getFacadeRoot());
var_dump($root);
You can check out the early docs for Laravel 4 here : http://four.laravel.com/ – that should give you a good starting point
The actual Laravel 4 code is well documented in the files. If you want to understand the inner workings then open up the source code files and read the notes. For example I looked up the DB::insert() code in /vendor/laravel/framework/src/Illuminate/Foundation/Application.php.
/**
* Run an insert statement against the database.
*
* #param string $query
* #param array $bindings
* #return bool
*/
public function insert($query, $bindings = array())
{
return $this->statement($query, $bindings);
}
Ok, so this is calling the statement function so I search for function statement in the same code / class:
/**
* Execute an SQL statement and return the boolean result.
*
* #param string $query
* #param array $bindings
* #return bool
*/
public function statement($query, $bindings = array())
{
return $this->run($query, $bindings, function($me, $query, $bindings)
{
if ($me->pretending()) return true;
$bindings = $me->prepareBindings($bindings);
return $me->getPdo()->prepare($query)->execute($bindings);
});
}
We can now see that this returns the boolean result based on the comments above the code.
If you come from Laravel 3 this article is for you. After that you should read the other tutorials of that series.
Author's note:
This article should outline some of the more important changes to Laravel between versions 3 and the upcoming version 4. Bear in mind
this isn’t all of the changes. As the release of Laravel 4 gets closer
I’ll keep this article up to date. If you’re having any problems with
Laravel 4 please jump on to #laravel on Freenode. At this time we’d
like to ask people not to post help topics on the forums.

Resources