Codeigniter database prefix add in table - mysql

I have one database in which all table names like below
configuration_dst,
developer_dst,
application_dst
Now I want to manage my query in which want to add database prefix after table name instead of before.
For example :
{TABLE NAME}{PREFIX}
is it possible to manage using CI 3.0 ?
I have tried like below in Application/configuration/database.php
$db['default']['dbprefix']="_dst";
$db['default']['swap_pre']="{POST}";
Current Query :
$this->db->get('templates');
Current Table Name :
tablename : _dsttemplates
Expected Table Name :
tablename : templates_dst
I need prefix after table Name not before but didn't get any solution.

The only option is to change in driver files. You can do this using the following steps:
Go to /system/database/DB_query_builder.php
Search public function dbprefix
Replace
return $this->dbprefix.$table;
with
return $table.$this->dbprefix;
If it also interferes in any other places in your project then create a new function with replaced code like:
public function dbsuffix($table = '')
{
if ($table === '')
{
$this->display_error('db_table_name_required');
}
return $table.$this->dbprefix;
}

Working with Database prefixes manually
if you use
$this->db->set_dbprefix('newprefix');
$this->db->dbprefix('tablename'); // outputs newprefix_tablename
its always gives the table name as
newprefix_tablename
Because this is Codeigniter pastern.
Codeigniter Prefix

Related

Automatically append database name to database table names

Is it possible to automatically append database name to a table name in laravel?
The issue is that I have to join data from multiple databases in single queries and sometime I am having to manually replace template names, which is a lot of hassle.
The only solution that I found is that I can append database name to the table name within a model, i.e.
class User extends Model
{
protected $table = 'database_name.table_name';
}
But with above we are losing support for table prefixes.
Example when database name is not applied:
$userQuery = User::where('id', 1)
->with('settings')
->select('some data');
DB::connection('x')
->table('table-on-different-connection')
->insertUsing(['some columns'], $userQuery);
$userQuery is on a different connection and database_name was not applied to the tables within that part of the query. Hence why insertUsing is trying to perform joins on connection x.
Laravel is not appending database name when generating SQL statements. To resolve that, you need to create your own MySQL wrapper and append the database name to the table name that way.
This is where the issue takes place:
vendor\laravel\framework\src\Illuminate\Database\Query\Grammar.php
public function wrapTable($table)
{
if (! $this->isExpression($table)) {
return $this->wrap($this->tablePrefix.$table, true);
}
return $this->getValue($table);
}
You need to override wrapTable method and append database name to the table that way.
i.e.
public function wrapTable($table)
{
$databaseName = $this->wrap('my_database'); // dynamically defined name here
if (! $this->isExpression($table)) {
$tableName = $this->wrap($this->tablePrefix.$table, true);
return "{$databaseName}.{$tableName}";
}
return $this->getValue("{$databaseName}.{$table}");
}
How you go about extending Grammar and override this method depends on your application and your needs. This can be done globally (i.e. via AppProvider) or for an individual query.

Symfony Doctrine: Search in simple_array

I got values stored in my database column field as value1,value2,value3,value4, so a simple_array column.
So i'm using Doctrine to make a search using this:
$searchQuery = $this->getDoctrine()
->getRepository('AppBundle:Ads')
->createQueryBuilder('p')
->andWhere("p.vals <= :value2")
->setParameter('value2', $request->query->get('value2'));
->orderBy("p.creationtime", 'DESC');
So expecting value2 is in the 2nd position of a simple array like value1,value2,value3, how can i ask QueryBuilder to select the second value in the string?
I think this query try to get all the values in p.vals, results are not right, shound select just one.
How can I select eg. the 2nd value in p.vals?
I believe you cannot access nth item of an array column using pure Mysql since the data is serialized, in order to do it I'd create a simple function
public function getItemFromArray(array $array, $index)
{
return isset($array[$index]) ? $array[$index] : null;
}
And if you want to find item with condition use
array_filter()

Laravel Schema Builder : Creating a binary(16) column

Using Laravel 5.5 and Mysql (10.1.19-MariaDB)
For a md5 hash I want a binary(16) column.
Let's call the colum url_hash
When using :
$table->binary('url_hash');
it will give me a BLOB column.
source : https://laravel.com/docs/5.5/migrations#creating-columns
I have seen all kind of hacks or plugins around the web for this , but what is the most simple one without any external plugins that could break on the next update?
Cheers
You can just set the character set to binary.
$table->char('url_hash', 16)->charset('binary');
This is actually shown as a real binary column type with a length of 16 in MySQL Workbench.
There shouldn't be any difference: https://stackoverflow.com/a/15335682/5412658
Extend the MySqlGrammar class, e.g. in app/MySqlGrammar.php:
namespace App;
use Illuminate\Support\Fluent;
class MySqlGrammar extends \Illuminate\Database\Schema\Grammars\MySqlGrammar {
protected function typeRealBinary(Fluent $column) {
return "binary({$column->length})";
}
}
Then use a macro to add your own column type:
DB::connection()->setSchemaGrammar(new \App\MySqlGrammar());
Blueprint::macro('realBinary', function($column, $length) {
return $this->addColumn('realBinary', $column, compact('length'));
});
Schema::create('table', function(Blueprint $table) {
$table->realBinary('url_hash', 16);
});
Laravel author recommends to do a DB:statement call and run the raw SQL.
If you are running migration, you could run this raw SQL after Schema::create:
DB::statement('ALTER TABLE table_name ADD url_hash binary(16) AFTER some_column');
Depends on use case, you could need to run this raw SQL to drop the column before dropping the table:
DB::statement('ALTER TABLE table_name DROP url_hash');

Not retrieving set of values from table. Laravel

I'm trying to get values from my linking table sfees with columns student_id and mfee_id. Here, there might be multiple student_id with different mfee_id. The thing is that, i want to retrieve all mfee_id with same student_id.
I have used following syntax, but it is only returning single value:
public function verify($id,$sid)
{
$sfees = sfee::where('student_id', $sid)->value('mfee_id');//trying to get only mfee_id
return $sfees;
}
How can i solve this problem?
//edited
My table looks like:
You need to do a groupBy -
$sfees = sfee::where('student_id', '=',$sid)->groupBy('student_id')->get();
UPDATE
Try something like this -
$sfees = sfee::where('student_id', '=',$sid)->lists('mfee_id');
Or you can use the Schema Builder like this -
DB::table('sfees')->where('student_id', '=', $id)->lists('mfee_id');

Change image name and update into same table using mysql query

I want to change image name in table like below.
image name : test.png
replace with : test_E.png
I want _E at end of all image name in table using mysql query.
Use replace function
update <table>
set image=replace(image,'.png','_E.png')
you could use this, if the image extension is not same in the table
update <table>
set image=concat(substring(image,1,locate('.',image)-1),'_E',
substring(image,locate('.',image),lenght(image)))
You can use string functions of MySQL query:
UPDATE TABLE SET IMAGE_NAME = CONCAT(SUBSTR(IMAGE_NAME,(CHAR_LENGTH(IMAGE_NAME) - 4)),
'_E' , SUBSTR(IMAGE_NAME, -4)) WHERE ID = <put record id>;
SUBSTR(IMAGE_NAME,(CHAR_LENGTH(IMAGE_NAME)-4)) would return name of file - assuming extension is of 3 chars. For 'test.png' above function would remove '.png' and function would return 'test'
SUBSTR(IMAGE_NAME, -4) would return last four chars of string - so 'test.png' would return '.png'
using concat you can concat 'test', '_E' and '.png' - returning 'test_E.png'
Please refer to string functions reference of MySQL for further use
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html