Query limitation in DBO (Joomla 3.8.1) - database

I use JDatabaseDriver for interaction with database.
The next code I took from official Joomla Documentation.
Documentation
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select($db->quoteName(array('user_id', 'profile_key', 'profile_value', 'ordering')))
->from($db->quoteName('#__user_profiles'))
->setLimit('10');
Look at the last row in code above.
The setlimit method is no exist in JDatabaseDriver class but it is declared in class - JDatabaseQueryMysqli.
Don't I understand the logic or there is some mistake?

In Latest Joomla 3.8.1 setLIMIT does not work properly. You may go for alternative methods like
$query->setQuery($query,start,offset);

Related

How to write these line of code in cakephp 3.x

I am very new in cakephp and i have to upgrade a cake project from version 1.1 to 3.6. I do not know how to convert these lines of code to cakephp 3.6:
App::import('Model', 'SystemMenu');
$system_menu =& new SystemMenu();
SystemMenu is an model which was define in Model folder.
Thank you very much for your help.
If youre within a controller, you can do
$this->loadModel('SystemMenus');
and access the model like so
$this->SystemMenus->find()->...
If not, you can use TableRegistry
$systemMenus = TableRegistry::get('SystemMenus')
And access is simple:
$systemMenus->find()->...
See https://book.cakephp.org/3.0/en/orm/table-objects.html for more information
Notice that i have changed the table name to be plural, as the CakePHP 3.x conventions specifies https://book.cakephp.org/3.0/en/intro/conventions.html
You can use TableRegistry class.
$system_menu = \Cake\ORM\TableRegistry::get('SystemMenu');
//new entity
$entity = $system_menu->newEntity();
//get entity by id
$entity = $system_menu->get(2);
//Save entity
$system_menu->save($e);
// finder
$menu = $system_menu->find()->toArray();

Auto generating a value during update

I am using ef-core 2.1 with MSSQL, in OnModelCreating I use the following which auto populates a created field correctly:
modelBuilder
.Entity<MyModel>()
.Property(e => e.Created)
.HasDefaultValueSql("GETUTCDATE()")
.ValueGeneratedOnAdd()
.Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;
Additionally, I want to populate (and update thereafter) another field as follows:
modelBuilder
.Entity<MyModel>()
.Property(e => e.Modified)
.HasDefaultValueSql("GETUTCDATE()")
.ValueGeneratedOnUpdate()
.Metadata.ValueGenerated = ValueGenerated.OnUpdate;
However, this has no effect. In reading value-generated-on-add-or-update it is not clear to me if no facility exists to support this and a trigger is the only option or I simply have the configuration wrong.
The fluent method calls appear to support this, anyone know what I am doing wrong?
Hmm, I am not real familiar with what you are trying with .Metadata.ValueGenerated, however, I am doing something very similar in my project with success (note that my db is MySQL).
Maybe try like below.
modelBuilder
.Entity<MyModel>()
.Property(e => e.Modified)
.HasDefaultValueSql("GETUTCDATE()")
.ValueGeneratedOnAddOrUpdate();

I want to show some values stored in database in a Joomla article

Hello everyone I want to show some values from the database in a joomla article in the following manner how can I do it ???
I've created the database in the following manner
One way to include php code in your article is to use an extension like
http://extensions.joomla.org/extension/sourcerer
This will allow you to put some php code that will make a query. Doing this in a Joomla way it would look like:
$db = JFactory::getDbo();
$query = $db->getQuery(true)
$query
->select($db->quoteName(array('ques', 'options', 'ans')))
->from($db->quoteName('#__yourtablename'))
->where($db->quoteName('id') . ' = ' . $idOfQuestionYouWantToGrab);
$db->setQuery($query);
$result = $db->loadAssoc();
Now you can show your question:
<?php echo $result['ques']; ?>
then somehow you will need to visualize questions separating them them with some regular expression, since your fields aren't structured well :/
Same with the answers. Then you will need to make some validation with JavaScript to see if answer is the correct one.

CakePHP read() is not returning a model name

I am having trouble with my cakePHP and wondering if anyone else has experienced this. I am trying to setup a User object. I create the Model:
class User extends AppModel
{
}
I create the controller:
class UsersController extends AppController
{
function view($id = null) {
$this->User->id = $id;
$this->set('users', $this->User->read());
}
}
and I go to the view page. However, I am not getting what the cake documentation says I should be getting. I am getting:
Array
(
[0] => Array
(
[id] => 3
[FirstName] => 1
[LastName] => 1
)
)
when what I am expecting to see is
Array
(
[User] => Array
(
[id] => 3
[FirstName] => 1
[LastName] => 1
)
)
Also when I do a $this->User->find('all'); I get back an array like so:
Array
(
[0] => Array ([0] => Array (/*stuff here*/))
[1] => Array ([0] => Array (/*stuff here*/))
[2] => Array ([0] => Array (/*stuff here*/))
)
I have tried changing the name to Myuser (including the database table, controller, model, etc) and still have the same results, so I don't think it's related to a reserved keyword.
Has anyone run into this or, more importantly, does anyone have a clue how I might fix it? Thanks.
EDIT:
I am using cake version 2.0.6. I am using a MySQL 5.0.92 database. I just have tried setting the name variable and it did not change my results.
After an ENTIRE DAY of troubleshooting, I finally was able to solve it.
The cause is definitely an outdated pdo_mysql.so library. This is located in /usr/local/lib/php/extensions/(latest directory)/pdo_mysql.so
The table name being returned in getColumnMeta was only added in a certain version due to this function request:
https://bugs.php.net/41416/
Now, the problem is, in some web hosts, PHP needs to be compiled with Easy Apache. Mine had to go through that as well just in order to enable PDO (it was initially disabled). But the problem is, for some reason, Easy Apache is downloading some obsolete source code everytime it runs. Running yum or installing any RPMs don't help either.
So here is what I did:
- I downloaded the latest PHP sources from the PHP site, extracted the tarball
- I ran Easy Apache, did a recompilation, and very quickly went to the console to watch it redownload the outdated PHP sources
- When the PHP sources have reappeared, I very quickly replaced the entire ext/pdo_mysql directory with the latest sources
- Easy Apache will compile httpd first, so you have some time to do the above step
- After the build is done, reboot.
- To check if your version of pdo_mysql.so supports the table name, execute this command:
strings -f pdo_mysql.so | grep ': table'
- There should be an entry there. The old version doesn't.
- By the way, I noticed that there are more copied of the pdo_mysql.so in /usr/lib/php/modules and /usr/lib/php/extensions, but it seems that the one in /usr/local is the one that is active. Nevertheless, I update all copies manually
NOTE: if you just try to update the pdo_mysql.so file, it will not work. You will get a segmentation fault, and the pages will render nothing. You need to recompile PHP using the above steps.
I hope this will help other people who will come across this bug.
Be sure your table is named users and include the association explicitly
class User extends AppModel {
public $name = 'User';
public $useTable = 'users';
...
You need to set the $uses variable to associate the User model with the controller if you are using any additional models. Adding it explicitly even if there's just one will not hurt either...
for example
class UsersController extends AppController {
//Controller Name
public $name = 'Users';
//DB Config for desired connection
public $useDbConfig = 'test';
// Array of associated models
public $uses = array('Store','User');
//Array of Helpers used by Controller Views
public $helpers = array('Html', 'Form');
...
Also, check to be sure there are no edits in AppModel or AppController which may be accidentally contributing to the interactions here... You may also consider dumping the value of the $uses to see what's there.
Finally found out that the reason for it has to do with a function called PDOStatement::getColumnMeta. This gets called on the queries that cakePHP runs is used by the framework to get the column name, column type, and table name. However, for whatever reason, on the webhost I currently have, this function does not return the table name so cakePHP defaults to creating the array with a 0 index rather than a table name index.
$column = $results->getColumnMeta($index);
if (!empty($column['table']) && strpos($column['name'], $this->virtualFieldSeparator) === false) {
$this->map[$index++] = array($column['table'], $column['name'], $type);
} else {
$this->map[$index++] = array(0, $column['name'], $type);
}
still not sure how to fix it yet, but this is definitely why it is happening.
Yes, you are right. beastmaster
The reason why is that u using old version of PDO driver which has been been deprecated. the PDOStatement::getColumnMeta does not show [table]=name in old version
So solution here download new version of PHP which has build-in PDO. DO NOT INSTALL PDO VIA PECL coz u getting old version of PDO extension. and using
<?php phpinfo ?>
to check if it is installed properly. so u can use native PDO driver coming with PHP rather than use extension PDO driver.
By the way, Thank you for point out the problem.
I have just faced the problem. The cause was not an outdated pdo_mysql.so library.
If you use cakephp 2.3.x and make sure to use PHP 5.2.8 or greater, you should check whether the pdo_mysql extension is enabled or disabled.
You can echo phpinfo() to check it.
Note: the pdo_mysql extension is only enabled if you see "PDO Driver for MySQL enabled", otherwise it is disabled.

CakePHP model useTable with SQL Views

I'm in the process converting our CakePHP-built website from Pervasive to SQL Server 2005. After a lot of hassle the setup I've gotten to work is using the ADODB driver with 'connect' as odbc_mssql. This connects to our database and builds the SQL queries just fine.
However, here's the rub: one of our Models was associated with an SQL view in Pervasive. I ported over the view, but it appears using the set up that I have that CakePHP can't find the View in SQL Server.
Couldn't find much after some Google searches - has anyone else run into a problem like this? Is there a solution/workaround, or is there some redesign in my future?
First of all, which version of CakePHP are you using? I'll assume it's about CakePHP 1.2+.
The problem
I'm not familiar with SQL Server 2005 (nor any other versions), but after some investigation, I think the problem is in DboMssql::listSources() method, which selects available table names from INFORMATION_SCHEMA.TABLES and so it doesn't "see" any available views.
The solution
Change DboMssql::listSources() to select available table names from sys.tables or, if I'm wrong about sys.tables, to additionally select names from INFORMATION_SCHEMA.VIEWS.
So, not to mess with CakePHP core files, you'll have to create custom datasource, which extends DboMssql and overrides ::listSources() method. To do so, you'll have to:
Create <path/to/app>/models/datasources/dbo/dbo_custom_mssql.php:
<?php
App::import('Datasource', 'DboMssql');
class DboCustomMssql
extends DboMssql
{
public
function listSources()
{
$cache = DboSource::listSources();
if ($cache != null) {
return $cache;
}
$result = $this->fetchAll('SELECT TABLE_NAME FROM SYS.TABLES', false);
if (!$result || empty($result)) {
return array();
} else {
$tables = array();
foreach ($result as $table) {
$tables[] = $table[0]['TABLE_NAME'];
}
DboSource::listSources($tables);
return $tables;
}
}
}
Change config/database.php config: 'driver' => 'custom_mssql'
Test
NB: The worst thing is that DboMssql::listSources() interface is kinda broken (w/o optional $data argument (like Datasource::listSources() declares)) and doesn't provide any point of extension, so, in order to have source list caching, we're forced to call DboSource::listSources() instead of broken parent::listSources().
I have SQL Server views being happily displayed. The main difference that I have to you is that I am using the mssql driver rather than the odbc_mssql driver. Perhaps you should try switching to that and figure out whatever problems you have there first.

Resources