I need to get prefix table in magento 2 to join with another table. I try to find on internet but I don't see how to get prefix table in magento 2. Can you help me
You can get the prefix by using the function getTableName() like below :
Always use getTableName with raw queries to fetch the table along with the prefix dynamically
protected $_resource;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\ResourceConnection $resource
) {
$this->_resource = $resource;
parent::__construct($context);
}
public function execute()
{
$connection = $this->_resource->getConnection();
$tableName = $connection->getTableName('customer_entity'); // It will return "yourtableprefix_customer_entity"
$mapsDeleteQuery = "your raw SQL Query";
$connection->query($mapsDeleteQuery);
}
It seems that there have been some code changes in Magento. So now referring to #Manashvi Birla answer, you need to replace :
$connection->getTableName('customer_entity');
by :
$this->_resource->getTableName('customer_entity');
to be able to get the table prefix.
Just open env.php. You can find env.php file under base_dir/app/etc/env.php
Here you can search keyword table_prefix.
I hope it helps you.
Related
I want get id value from eloquent laravel and passing to variable to insert process. this is my eloquent to get data from siswa table
$siswa = Siswa::where('nis', $row['nis'])->first();
but when accessing id value like $siswa->id I'm getting error
enter image description here
Use this code
if($siswa){
$id=$siswa->id;
}
$siswa variable can be null. Look at its contents with the dd($siswa) command.
To escape the error you can use it like this:
if($siswa)
{
// $siswa not null.
}
You have two ways to fix this.
1/ You can use ->firstOrFail() to display a 404 page in case of your data is not found :
$siswa = Siswa::where('nis', $row['nis'])->firstOrFail();
// if $siswa is NULL, then it will display a 404 page, else it will continue
2/ You can add a simple condition to continue with your data :
$siswa = Siswa::where('nis', $row['nis'])->first();
if ($siswa) {
// then continue
}
First you can $id pass on your public function like
public function show($id)
{
$siswa = Siswa::where('id', $id)->first();
}
You can pass your id on your route
Is is possible to modify a request before it gets inserted into the database?
public function store(StoreRequest $request)
{
$request->date_posted = strtotime($request->date_posted);
//insert data here.
}
Yes it can be, what you have works perfectly fine
public function store(StoreRequest $request)
{
$request->date_posted = strtotime($request->date_posted);
or
$datePosted = $request->date_posted + 2;
$datePosted = $request->date_posted . 'some other addons';
//insert data here.
}
these are just examples but i hope you get what im saying.
Fyi if you input into the db, the created_at and updated_at will update and so you don't need to insert the time the post was made manually.
Try this solution maybe can resolve your issue . Add setter in model so it will be look like this
Class ModelX {
public function setDateDatePosted Attribute($date_posted)
{
$this->attributes['date_posted'] = strtotime($date_posted);
}
}
hope it useful for you .
In joomla 3.x implemented delete the data from database using object
this my code not working
function deleteJob()
{
$id = $_REQUEST['id_ud'];
$object = new stdClass();
$object->id = $id;
$resultd =JFactory::getDbo()->deleteObject('#___institute_post_job',$object, 'id');
}
if(isset($_POST['inidelete_job'])) {
deleteJob();
}
please help me?
There is No method for "deleteObject" . if you want to delete operation , you have write query and execute.
There is no object delete just like insertObject,updateObject,...
refer last example for delete:
https://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase
JFactory::getDBo() basically call JDatabaseDriver,you can use following methods.
https://api.joomla.org/cms-3/classes/JDatabaseDriver.html
i want to change my codigniter url which looks like
http://dev.hello.com/about/updateVision/9
to
http://dev.hello.com/about/updateVision/this-is-test-edit/
i have no idea how to make looks like that anyone can help please
thanks !
This all depends on you database structure. I would introduce a field called reference or slug. This will contain a unique string for your record. When creating the record you can use the url_title function to remove spaces as mentioned by #Rooneyl
This reference must be unique so I would create a function that creates a reference in a loop, checks it against the database. When it returns false, it is unique.
public function unique_reference($name)
{
$name = trim(strtolower($name));
$unique_reference = url_title($name);
$counter = '';
do {
$result = $this->db->select('id')
->from('your_table')
->where('reference', $unique_reference)
->get()->row();
if ($result)
$counter++;
$unique_reference = url_title(trim("{$name} {$counter}"));
} while ($result);
return $unique_reference;
}
You can then retrieve record using this unique value
I'm working with the latest codeIgniter released, and i'm also working with jquery datatables from datatables.net
I've written this function: https://gist.github.com/4478424 which, as is works fine. Except when I filter by using the text box typing something in. The filter itself happens, but my count is completely off.
I tried to add in $res = $this->db->count_all_results() before my get, and it stops the get from working at all. What I need to accomplish, if ($data['sSearch'] != '') then to utilize the entire query without the limit to see how many total rows with the search filter exists.
If you need to see any other code other than whats in my gist, just ask and I will go ahead and post it.
$this->db->count_all_results() replaces $this->db->get() in a database call.
I.E. you can call either count_all_results() or get(), but not both.
You need to do two seperate active record calls. One to assign the results #, and one to get the actual results.
Something like this for the count:
$this->db->select('id');
$this->db->from('table');
$this->db->where($your_conditions);
$num_results = $this->db->count_all_results();
And for the actual query (which you should already have):
$this->db->select($your_columns);
$this->db->from('table');
$this->db->where($your_conditions);
$this->db->limit($limit);
$query = $this->db->get();
Have you read up on https://www.codeigniter.com/userguide2/database/active_record.html#caching ?
I see you are trying to do some pagination where you need the "real" total results and at the same time limiting.
This is my practice in most of my codes I do in CI.
$this->db->start_cache();
// All your conditions without limit
$this->db->from();
$this->db->where(); // and etc...
$this->db->stop_cache();
$total_rows = $this->db->count_all_results(); // This will get the real total rows
// Limit the rows now so to return per page result
$this->db->limit($per_page, $offset);
$result = $this->db->get();
return array(
'total_rows' => $total_rows,
'result' => $result,
); // Return this back to the controller.
I typed the codes above without testing but it should work something like this. I do this in all of my projects.
You dont actually have to have the from either, you can include the table name in the count_all_results like so.
$this->db->count_all_results('table_name');
Count first with no_reset_flag.
$this->db->count_all_results('', FALSE);
$rows = $this->db->get()->result_array();
system/database/DB_query_builder.php
public function count_all_results($table = '', $reset = TRUE) { ... }
The
$this->db->count_all_results();
actually replaces the:
$this->db->get();
So you can't actually have both.
If you want to do have both get and to calculate the num rows at the same query you can easily do this:
$this->db->from(....);
$this->db->where(....);
$db_results = $this->get();
$results = $db_results->result();
$num_rows = $db_results->num_rows();
Try this
/**
* #param $column_name : Use In Choosing Column name
* #param $where : Use In Condition Statement
* #param $table_name : Name of Database Table
* Description : Count all results
*/
function count_all_results($column_name = array(),$where=array(), $table_name = array())
{
$this->db->select($column_name);
// If Where is not NULL
if(!empty($where) && count($where) > 0 )
{
$this->db->where($where);
}
// Return Count Column
return $this->db->count_all_results($table_name[0]);//table_name array sub 0
}
Then Simple Call the Method
Like this
$this->my_model->count_all_results(['column_name'],['where'],['table name']);
If your queries contain a group by, using count_all_results fails. I wrote a simple method to work around this. The key to preventing writing your queries twice is to put them all inside a private method that can be called twice. Here is some sample code:
class Report extends CI_Model {
...
public function get($page=0){
$this->_complex_query();
$this->db->limit($this->results_per_page, $page*$this->results_per_page);
$sales = $this->db->get()->result(); //no table needed in get()
$this->_complex_query();
$num_results = $this->_count_results();
$num_pages = ceil($num_results/$this->results_per_page);
//return data to your controller
}
private function _complex_query(){
$this->db->where('a', $value);
$this->db->join('(subquery) as s', 's.id = table.s_id');
$this->db->group_by('table.column_a');
$this->db->from('table'); //crucial - we specify all tables here
}
private function _count_results(){
$query = $this->db->get_compiled_select();
$count_query = "SELECT count(*) as num_rows FROM (".$query.") count_wrap";
$r = $this->db->query($count_query)->row();
return $r->num_rows;
}
}