I a, trying to implement a modification to the registration process in opencart. I have modified the form in the view and the values are posted to the account/customer.php model in which I have added some code to process my new fields as shown below:
/* add children and links to product categories */
$this->load->model('account/children');
//loop childrens names to use key for remaining info
foreach($data['children-name'] as $key=>$name){
//re-assign data and purify
$child['name'] = mysql_real_escape_string($name);
$child['surname'] = mysql_real_escape_string($data['children-surname'][$key]);
$child['gender'] = mysql_real_escape_string($data['children-gender'][$key]);
$child['dob'] = mysql_real_escape_string($data['children-dob'][$key]);
$child['school'] = mysql_real_escape_string($data['children-school'][$key]);
$child['unit'] = mysql_real_escape_string($data['children-unit'][$key]);
$child['height'] = mysql_real_escape_string($data['children-height'][$key]);
$child['chest'] = mysql_real_escape_string($data['children-chest'][$key]);
$child['waist'] = mysql_real_escape_string($data['children-waist'][$key]);
$child['waistToKnee'] = mysql_real_escape_string($data['children-waist-to-knee'][$key]);
$child['insideLeg'] = mysql_real_escape_string($data['children-inside-leg'][$key]);
$child['shoeSize'] = mysql_real_escape_string($data['children-shoe-size'][$key]);
$child['customerId'] = $customer_id;
//update/create child record
$this->model_account_children->addChild($child);
}
/* end add children and links to product categories */
I then created the relative model file in account/children.php in which has the following code:
class ModelAccountChildren extends Model {
public function addChild($data) {
//create other fields not in $data
$lastUpdated = date('Y-m-d h:i:s');
$childCat = getChildCategory($data['school']);
$dob = date('Y-m-d h:i:s', $date['dob']);
if($data['gender'] == 'm'){
$data['gender'] = 'Male';
}else{
$data['gender'] = 'Female';
}
echo "INSERT INTO " . DB_PREFIX . "customer_children (customer_id, child_name, child_surname, child_gender, child_dob, category_id, child_school, child_unit, child_height, child_chest, child_waist, child_waist_to_knee, child_inside_leg, child_shoe_size, child_last_updated) VALUES (".$data['customerId'].", '".$data['name']."', '".$data['surname']."', '".$data['gender']."', '".$dob."', ".$childCat['category_id'].", '".$data['school']."', '".$data['unit']."', '".$data['unit']."', '".$data['height']."', '".$data['chest']."', '".$data['waist']."', '".$data['waistToKnee']."', '".$data['insideLeg']."', '".$data['shoeSize']."', '".$lastUpdated."')";
//perform insert
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_children (customer_id, child_name, child_surname, child_gender, child_dob, category_id, child_school, child_unit, child_height, child_chest, child_waist, child_waist_to_knee, child_inside_leg, child_shoe_size, child_last_updated) VALUES (".$data['customerId'].", '".$data['name']."', '".$data['surname']."', '".$data['gender']."', '".$dob."', ".$childCat['category_id'].", '".$data['school']."', '".$data['unit']."', '".$data['unit']."', '".$data['height']."', '".$data['chest']."', '".$data['waist']."', '".$data['waistToKnee']."', '".$data['insideLeg']."', '".$data['shoeSize']."', '".$lastUpdated."')");
}
public function updateChild($data) {
}
public function getChildCategory($childSchoolStr){
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_description WHERE name LIKE '" . $childSchoolStr . "'");
return $query->row;
}
}
For some reason the script is not getting access to the new model at the point I call it in model/account/customer.php on the line:
$this->load->model('account/children');
Am I doing something wrong?
Worked it out chaps.
$childCat = getChildCategory($data['school']);
Bit of a brain lapse, line above should be:
$childCat = $this->getChildCategory($data['school']);
It seems correct to me. You can use it within model. Do you use vqmod? Try deleting cache files.
Maybe add a test function to your model and see you can call it.
What is the error you get?
Have you definitely put the file in catalog/model/account/children.php ? If so, do you get any error messages at all that would help debug this? Also, when you run the code, are you certain that $data['children-name'] contains data?
Related
Am displaying list haviing
Date
News Heading
Short Descrption
The list is spread to around 100 pages, having 20 news in each page
Issue is: This is working absolute fine in php 7.3, 7.4 in joomla 3.10 where on clicking url - list is shown spread over multiple pages having sort by date wise as first criteria, latest date of publishing is coming
But when same used on php 8.0.x - its showing incorrectly, where on clicking URL - last page of list having page number 100 is shown first. Now when i add on limitstart=0 in url then its showing correctly as the first page.
Now when i change from descending to ascending - its bringing the content on last page and its opening, but page number is 100 again
Seems like URL when opened is directly taking to last page of news item as published (although no limitstart is mentioned in it), which is incorrect as ideally should open in descending order and open the page having latest one
Below is code of views/list/tmpl/default.php
if(count($this->items) >0){
//$i=1;
foreach($this->items as $newslist)
{
$date = JFactory::getDate($newslist->n_date);
$list .='<h3><strong>'.$newslist->v_heading.'</strong></h3>
<p>'. $date->format('F j, Y').'</p>
<p>'.substr($newslist->v_short_description,0,100).'</p>
<p><i>Know More on:- </i><b><i>'.$newslist->v_heading.'</i></b></p><hr/><br>';
//$i=$i+1;
}
}else{
JError::raiseError(404, "Message");
}
<?php echo $list?>
and for models/list.php this is the function
protected function getListQuery()
{
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query
->select(
$this->getState(
'list.select', 'DISTINCT a.*'
)
);
$query->from('`#__news` AS a');
if (!JFactory::getUser()->authorise('core.edit', 'com_news'))
{
$query->where('a.state = 1');
}
// Filter by search in title
$search = $this->getState('filter.search');
if (!empty($search))
{
if (stripos($search, 'id:') === 0)
{
$query->where('a.id = ' . (int) substr($search, 3));
}
else
{
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.n_heading LIKE ' . $search . ' )');
}
}
/*
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering');
$orderDirn = $this->state->get('list.direction');
if ($orderCol && $orderDirn)
{
$query->order($db->escape($orderCol . ' ' . $orderDirn));
}
*/ //Order by date
$query->order ('a.n_date DESC');
$query->order ('a.id DESC');
return $query;
}
This is the code for views/list/view.html.php
public function display($tpl = null)
{
$app = JFactory::getApplication();
$this->state = $this->get('State');
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');
$this->params = $app->getParams('com_news');
$this->filterForm = $this->get('FilterForm');
$this->activeFilters = $this->get('ActiveFilters');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
throw new Exception(implode("\n", $errors));
}
$this->_prepareDocument();
parent::display($tpl);
}
Unsure how to achieve in and why its not working in php 8.0 where url should open the 1st page and not last page
function givemeposts($user, $pos){
$sql = "select * from posts where user ='" . $user . "' and pos = '" . $pos . "' order by inde";
...
givemeposts('public', 'home01'); // this works as usual
Now I nedd to call the above function, but without $pos param, i.e. whatever $pos value is.
Something like:
givemeposts('public', whatever);
Any way to do this?
You can use default parameter :
function givemepost( $user , $pos=NULL ){
if($pos==NULL){
//some code
}lese{
$sql = "select * from posts where user ='" . $user . "' and pos = '" . $pos . "' order by inde";
}
}
This way you can call the function with one paramter, and set what the function have to do, inside the if branch.
In PHP you can set default values for arguments
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.
If you do this, the default value for $pos will be '' (an empty string):
function givemeposts($user, $pos = ''){
Or you can something else than an empty string, whatever value you want:
function givemeposts($user, $pos = 'SomeValue'){
Then you can simply call your function like this:
givemeposts($user);
You can try this:
function givemeposts($user, $pos = '')
{
if(!empty($pos)) {
$pos = "AND pos = '" . $pos . "'";
}
$sql = "SELECT * FROM posts WHERE user ='" . $user . "' $pos ORDER BY inde";
}
givemeposts('public');
public function test_passing_string() {
$this - > load - > model(array('registration/Registration_model', 'Jawaban_lab_model'));
$registration = new Registration_model();
$jawaban_lab = new Jawaban_lab_model();
$id = "kuda4";
$jawaban_lab - > load($id); //load jawaban_lab from id
$manualy_written_registration_number = "REG/FM/130102-0001";
echo "registration number from jawaban_lab->registration_number : ".$jawaban_lab - > registration_number
.
"<br> registration number from manualy_written_registration_number : ".$manualy_written_registration_number;
//$registration->load($jawaban_lab->registration_number);
$registration - > load($manualy_written_registration_number);
echo "<br> patient id : ".json_encode($registration - > PatientID);
}
Before go to the question, I will explain my code.
On test_passing_string() function, I call 2 model, and create object for each model there are $registration and $jawaban_lab.
To load data from model I create a load() function. load() has two parameters: column_value and column_name. The default value for column_name is that model's Primary Key.
BUT
The problem comes from
$registration->load($jawaban_lab->registration_number);
I can't retrieve any $registration object data, then I test it by passing the value manually by write this:
$manualy_written_registration_number = "REG/FM/130102-0001";
$registration - > load($manualy_written_registration_number);
And the result appear, doesn't that mean my load() function is fine?
Then I check value inside $jawaban_lab->registration_number by echoing it, surprisingly it display same value as my $manualy_written_registration_number variable.
This is screenshoot in my browser when I run test_passing_string() function:
Using $manualy_written_registration_number value
Using $jawaban_lab->registration_number value
Why can't I use the value from
$jawaban_lab->registration_number even though it has the same value as
my manually writen registraiton number?
public function load($column_value, $column_name = NULL) {
$query = NULL;
if ($column_name != NULL) {
// using custom column.
$query = $this->dbs->get_where($this::DB_TABLE, array(
$column_name => $column_value
));
} else {
// using column primary key .
$query = $this->dbs->get_where($this::DB_TABLE, array(
$this::DB_TABLE_PK => $column_value
));
}
if ($query->row()) {
$this->populate($query->row());
}
}
I use multiple database using CodeIgniter 3, registration_model from SQL server and jawaban_lab from MySQL, jawaban lab have column registration_number to store registration_model primary key
var_dump
First of all thanks to rlanvin and Nirajan N Raju
from rlanvin's comment, i find out the problem is come from codeigniter's query helper, because when i enable codeigniter profiling sql server query return "SELECT CASE WHEN (##OPTIONS | 256) = ##OPTIONS THEN 1 ELSE 0 END AS qi"
so i think codeigniter might be cannot generate query so i create the query manually
i change
public function load($column_value, $column_name = NULL) {
$query = NULL;
if ($column_name != NULL) {
// using custom column.
$query = $this->dbs->get_where($this::DB_TABLE, array(
$column_name => $column_value
));
} else {
// using column primary key .
$query = $this->dbs->get_where($this::DB_TABLE, array(
$this::DB_TABLE_PK => $column_value
));
}
if ($query->row()) {
$this->populate($query->row());
}
}
to this
public function load($column_value, $column_name = NULL) {
$query = NULL;
if ($column_name != NULL) {
$query = $this->dbs->query("SELECT * FROM " . $this::DB_TABLE . " WHERE " . $column_name . " LIKE '" . trim($column_value) . "'");
} else {
$query = $this->dbs->query("SELECT * FROM " . $this::DB_TABLE . " WHERE " . $this::DB_TABLE_PK . " LIKE '" . trim($column_value) . "'");
}
if ($query->row()) {
$this->populate($query->row());
}
}
This question already has answers here:
PHP Objects vs Arrays -- Performance comparison while iterating
(11 answers)
Closed 7 years ago.
As I know, an array in php can designed by key and value.
e.q.
$person = array(
'name'=>'JOSH',
'age'=>18
);
It likes an object but just defined by another way.
e.q.
class info{
$name;
$age;
}
$person = new info();
$person->name = 'JOSH';
$person->age = 18;
I always use array in PHP, but some of my co-workers they say the object type is better than array type.
But I don't understand what different between array type and object type.
Can someone tell me what different between this two types if I just want to declare variable in PHP ?
There are a lot of differences, but the object is more powerfull.
Is in a way to avoid rewrite code, and clean the code.
Think you want to do some operations to numbers in an array:
To keep it simple, I suppose you already get the values from array or object.
<?
$item = array(
'name'=>'carrot',
'price'=>0.20,
'stock' => 15
);
?>
For example, in an shop context, you want to get the price befor buy, and update the stock.
<?
function getprice($item){
return $item['price'];
}
function substock($item,$units){
$item['stock'] = $item['stock'] - $units;
}
echo getprice($item);
echo "<br/>";
echo substock($item,"3");
?>
It will output something like:
0.20
12
That can be a way, but what can we do with the objects:
<?
class items{
var $name , $price, $stock;
function __construct($in_name, $in_price, $in_stock){
if (!empty($in_name)){$this->name = $in_name;}
if (!empty($in_price)){$this->price = $in_price;}
if (!empty($in_stock)){$this->stock = $in_stock;}
}
function getprice(){
return $this->price;
}
function substock($units){
$newstock = $this->stock - $units;
$this->stock = $newstock;
return $newstock;
}
}
$item = new items("carrot","0.20","15");
echo $item->getprice();
echo "<br/>";
echo $item->substock("3");
?>
It will output something like:
0.20
12
Till this point is not a prety much difference, is'n it?
But imagine you want to create a bigger thing. Just play around with it.
Now I want to load an item just with the name of the carrot.
Then changing the method construct to be able to create an objet with different inputs:
var $name , $price, $stock;
function __construct($in_name, $in_price=NULL, $in_stock=NULL){
$args = func_num_args();
if ($args == 1){
$this->name = $in_name;
$this->fromdb($in_name);
}else{
if (!empty($in_name)){$this->name = $in_name;}
if (!empty($in_price)){$this->price = $in_price;}
if (!empty($in_stock)){$this->stock = $in_stock;}
}
}
function fromdb($name){
$sql = "SELECT * FROM items WHERE name = '" . $name . "'";
//... here we bring from database the item and put in an array called $itemdb.I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
$this -> price = $itemdb['price'];
$this -> stock = $itemdb['stock'];
}
function getprice(){
return $this->price;
}
function substock($units){
$newstock = $this->stock - $units;
$this->stock = $newstock;
return $newstock;
}
}
$item = new items("carrot");
echo $item->getprice();
echo "<br/>";
echo $item->substock("3");
?>
If the value in the database is same as the example before.It will output something like:
0.20
12
But from here you have infinite posibilities. Just play more.
Give a family item, and creating new methods.
<?
class items{
var $name , $price, $stock, $family;
function __construct($in_name, $in_price=NULL, $in_stock=NULL, $in_family=NULL){
$args = func_num_args();
if ($args == 1){
$this->name = $in_name;
$this->fromdb($in_name);
}else{
if (!empty($in_name)){$this->name = $in_name;}
if (!empty($in_price)){$this->price = $in_price;}
if (!empty($in_stock)){$this->stock = $in_stock;}
if (!empty($in_family)){$this->family = $in_family;}
}
}
function fromdb($name){
$sql = "SELECT * FROM items WHERE name = '" . $name . "'";
//... here we bring from database the item and put in an array called $itemdb. I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
$this -> price = $itemdb['price'];
$this -> stock = $itemdb['stock'];
$this -> family = $itemdb['family'];
}
function getprice(){
return $this->price;
}
function getfamily(){
return $this->family;
}
function substock($units){
$newstock = $this->stock - $units;
$this->stock = $newstock;
return $newstock;
}
function veggiesinfamily(){
$sql = "SELECT count(name),family FROM items WHERE family = '" . $this->family . "'";
//... here we bring from database the number of item of a family product in $number. I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
return $number;
}
function familystock(){
$sql = "SELECT SUM(stock),family FROM items WHERE family = '" . $this->family . "'";
//... here we bring from database the sum of stock items of a family product in $number. I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
return $number;
}
}
$item = new items("carrot");
echo "There are " . $item->veggiesinfamily() . $item->getfamily() . " kinds.<br/>";
echo "There are " . $item->familystock() . " units of " . $item->getfamily();
?>
We have also in our database an item: potato, 0.3, 10, roots (name, price, stock, family)
If the value in the database have as family roots and as elements carrot and potatoe. The output will be.
There is 2 roots kinds.
There is 25 units of roots.
And so on.
If you load the objet from an external file as item_class.php, and load as include("item_class.php"), can grow your script easily.
Cheers.
Softdelete behavior works fine on execute delete statement via the entity manager as the following code:
$entity = $this->em->getRepository('Users')->find(7);
$this->em->remove($entity);
$this->em->flush();
but when execute the same functionality via QueryBuilder hard delete will execute on database
$qb = $this->em->createQueryBuilder();
$qb->delete('Users', 'p');
$qb->where($qb->expr()->eq('p.id', ':id'));
$qb->setParameters(array("id" => 7));
$result = $qb->getQuery()->getResult();
How can I allow softdelete in all cases either via entity manager or query builder
If you use DQL then you have to use a Query Hint. This should do the trick:
$query = $qb->getQuery()
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
);
$result = $query->getResult();
Update:
The docs mention that you have to use a Query Hint but don't provide an example so I pulled the usage from their tests.
Docs: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/softdeleteable.md
Test Usage: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php
my old solution after previous answer by #Ken Hannel is:
Edit:
/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php
Replace walkDeleteClause function as the following:
public function walkDeleteClause(AST\DeleteClause $deleteClause)
{
$class = $this->em->getClassMetadata($deleteClause->abstractSchemaName);
$tableName = $class->getTableName();
$sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform);
$this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
$this->rootAliases[] = $deleteClause->aliasIdentificationVariable;
//check if SoftDeleteableListener is attached
foreach ($this->em->getEventManager()->getListeners() as $eventName => $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof \Gedmo\SoftDeleteable\SoftDeleteableListener) {
$date = date('Y-m-d H:i:s');
$sql = 'UPDATE ' . $this->quoteStrategy->getTableName($class, $this->platform) . " SET deletedAt = ' " . $date . " ' ";
}
}
}
return $sql;
}
but really but I think Ken Hannel way is more professional and up to standard.