How can I write this query to a code igniter query?
WHERE
SmallVersion.ID =
(
SELECT ChildSmallVersionID AS ID FROM SmallVersion
WHERE ID = $id
)
this is how is looks so far:
->where('SmallVersion.ID', ("SELECT `ChildSmallVersionID` AS ID
FROM `SmallVersion` WHERE ID = $id"));
Mateusz Wojtula's answer looks ok but you can protect your query
$strSubQuery = $this->db
->select("ChildSmallVersionID AS ID")
->from("SmallVersion")
->where("ID",$id)
->get_compiled_select();
$where = "SmallVersion.ID = (".$strSubQuery.")";
$this->db->where($where, NULL, FALSE);
There is no possibility to create brackets in Active Records but you can write it directly in your clause:
$where = "SmallVersion.ID =
(
SELECT ChildSmallVersionID AS ID FROM SmallVersion
WHERE ID = $id
)";
$this->db->where($where, NULL, FALSE);
If third parameter is set to FALSE, CodeIgniter will not protect your query.
Documentation for active records:
https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-data
if you want to apply this query
(
SELECT ChildSmallVersionID AS ID FROM SmallVersion
WHERE ID = $id
)
can make pice of query
$this->db->group_start()->select('ChildSmallVersionID AS ID')->where('ID', $id)->group_end();
and let make this query to string
$_sub_query = $this->db->group_start()->select('ChildSmallVersionID AS ID FROM SmallVersion')->where('ID', $id)->group_end()->get_compiled_select('SmallVersion');
and apply like
->where('SmallVersion.ID', $_sub_query);
Related
in Symfony i need access to another Database from another Project. So i created another entityManager for this connection.
Now i want read some Data where id field is uuid.
the statement looks like this:
$entityManager = $this->doctrine->getManager('myProject');
$connection = $entityManager->getConnection();
$statement = $connection->prepare(
'SELECT * FROM salutation '
);
$resultSet = $statement->executeQuery();
dd($resultSet->fetchAll());
But the id value is not readable.
"id" => b"\x0E2#\x05P\x08Gó€dˆk§\vè\x05"
Is there a way to tell Doctrine to handle this field as Uuid?
Thanks for your Help.
I want to select those uniname with same seller_id.There can be multiple uniname with same seller id. I am getting the seller_id info from html, where seller_id is passed as id. How to call it through controller for printing. The line I used is,
$university = DB::table('universities')->where('seller_id', '$id');
public function manage_seller_profile($id)
{
$divisions = Division::orderBy('id','asc')->get();
$districts = District::orderBy('id','asc')->get();
$seller_profile = Seller_info::find($id);
$university = DB::table('universities')->where('seller_id', '$id');
return view('Backend.pages.seller.manage_seller_profile', compact('seller_profile', 'districts', 'divisions', 'university'));
}
You need to remove these single quotes, and add the Laravel ORM select and get functions.
Your code will look like this:
$university = DB::table('universities')->where('seller_id', '=', $id)->select('uniname')->get();
I put the '=' operator to make the code more readable.
here is my code that i write in route file
`Route::get('/{username}/ajax-lead', function(){
$brand_id = Input::get('brand_id');
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
$table_lastRecords = DB::table($table_name)->get();
$columns = Schema::getColumnListing($table_name);
$table_lastRecords = DB::table($table_name)->get();
return Response::json($table_lastRecords);
});`
here is my view file
`{{Form::open(array('url'=>'','files'=>true))}}
client list *
Select Clients
#foreach($brand as $userLeads){
brand_id }}">{{ $userLeads->name }}
}
#endforeach
{{Form::close()}}
$('#brand_name').on('change',function(e){
// body...
console.log(e);
var brand_id = e.target.value;
//ajax Call
$.get('/{username}/ajax-lead?brand_id=' + brand_id,function(data){
//success data
console.log(data);
});
});
`
i want to get table_name column value in response. i use json for response.
ANSWER: Your easiest solution would be to return an array of data, where the first value is the table_name and the second value is the collection of lastRecords. It would look something like this:
$responseArray[0] = $table_name;
$responseArray[1] = $table_lastRecords;
return json_encode($responseArray);
Then, in your view, you would parse your Json response and let table_name = responseData[0] and lastRecords = responseData[1]. Of course, you could also use non-numeric indices to make the code more readable, such as responseData['table_name'], but those semantics are up to you.
ADDITIONAL INFO:
With that said, there are several issues with your code that I feel I should point out. You should become more familiar with Eloquent queries, as using them properly will greatly simplify your code. For instance, these two lines:
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
Could be re-written as:
$table_name = User::where('brand_id',$brand_id)->first()->table_name;
But this makes me ask the question, is there only one table per brand? If so, why do you store this information with the user, and not in a 'brands' table? The way that this would make the most sense would be to have a Brands model that has a table_name column, and you would get the table name with this simple query:
$table_name = Brand::find($brand_id)->table_name;
In addition, you should avoid doing this type of work in your routes file. Anything not related specifically to ROUTING the user should be handled in the controller or model. Then, within your controller, you should be utilizing the User or Brand model in your queries by importing App\User and App\Brand.
How can the admin user be informed when he create a product or category that the url key already exist with an error message and block the product/category creation until he change the product/category name or url key?
Thanks.
I have implemented a solution for this issue. I will post here a summary answer, if somebody have some questions, please ask.
Category: create an observer and for 'catalog_category_save_before' event add a url check validation, can be done with direct sql, something like:
$query = " SELECT value_id
FROM catalog_category_entity_varchar
WHERE value = '" . $urlKey . "' AND attribute_id = (
SELECT attribute_id
FROM eav_attribute
WHERE entity_type_id = 3 AND attribute_code = 'url_key')
";
Products: 2 things here...one to check the url when it's created from product name (if url key is empty on save product) and another one if url key is specified on product save:
add a backend model for the product 'name' attribute and on validate() method check for duplicate value if url key is not set, something like:
$urlKey = $object->getData('url_key');
if ($urlKey) {
return true;
}
if (!$this->getAttribute()->getEntity()->checkAttributeUniqueValue($this->getAttribute(), $object)) {
throw Mage::exception('Mage_Eav',
Mage::helper('eav')->__('The value of this attribute must be unique if you do not set explicit URL Key')
);
}
ovewrite in config.xml the urlkey backend model and the same like for 'name' attribute, add under validate() method the duplicate check, like:
if (!$this->getAttribute()->getEntity()->checkAttributeUniqueValue($this->getAttribute(), $object)) {
$label = $this->getAttribute()->getFrontend()->getLabel();
throw Mage::exception('Mage_Eav',
Mage::helper('eav')->__('The value of attribute "%s" must be unique', $label)
);
}
I have a table of customers where i need to generate unique id for each customer manually rather auto increment it in mysql. How to achieve this? I was suggested the below code with uniqid(md5(spl_object_hash($custApp)), false); function:
$data = file_get_contents("php://input");
$custApp = json_decode(base64_decode($data));
$custApp->Id = uniqid(md5(spl_object_hash($custApp)), false);
But i am not getting how to use this function in my code
use sha1($nombre_costumer) and save the value how your id unique