I'm looking at porting over some functions from the CakePHP 2.0 Users plugin into CakePHP 3.0. I've encountered this line in the old code:
$user = $this->{$this->modelClass}->passwordReset($this->request->data);
I debugged $user here and got a huge object filled with all kinds of data that I need. Perfect:
'properties' => [
'password' => '*****',
'id' => '53f7b636-e558-4eef-9064-1e78494ef653',
'username' => 'blahblah',
...
]
I thought everything was working perfectly until the next line:
$Email->to($user[$this->modelClass]['email'])
This is the first line of an email about to send. Here, $user[$this->modelClass]['email'] returns null. So I tried accessing the object directly, like so:
debug($user['properties']['email'])
This still returns null, I'm assuming because I'm trying to access parameters from a model. I'm not exactly sure what is different in CakePHP 3.0 for $this->modelClass or if I goofed up elsewhere. Any ideas?
I should be $user->email, as the new ORM now returns objects instead of arrays
Related
I have this function that receives a "user" model by parameter , I want to collect the properties of that object, for this I do it this way, the code works for me but the editor "phpstorm" complained to me with this error and it was to know what would be the best way to do this.
Thank you
public function sendEmail(User $user)
{
$params = [
'name' => "{$user->completeName}",
'language' => "{$user->locale}",
'user_id' => "{$user->id}"
];
}
Field accessed via magic method less... (Ctrl+F1)
Inspection info: Referenced field is not found in subject class. Note: Check is not performed on objects of type "stdClass" or derived.
Thanxs,
maybe this is simpler
$params = $user->toArray();
or
$params = $user->toJson();
That's because in Laravel your model does not actually have the properties defined.
In PHP there is the concept of magic methods though ( http://php.net/manual/en/language.oop5.overloading.php#object.get ), where the __get() method allows you to basically intercept the access to an otherwise inaccessible (or non-existing) property.
This is what happens behind the scenes in Laravel. All your property accesses are "intercepted" and Laravel looks if your database contains a column which is named like the property you are trying to access (very simplified speaking).
In a Laravel context you can savely ignore this warning.
I have build my own mvc framework and i'm using Doctrine DBAL 2.3 as database layer.
At the moment i'm working on a profiler for this framework.
One of the things i want to put in the profiler is the number of querys that were executed on the current page.
My question is: can i get the number of querys from Doctrine?
Yes? How can i do this?
No? Is there a way to build something custom that works with Doctrine and does the trick?
I hope there is someone that can answer my question, thank you.
Doctrine 2 provides a simple interface for logging, namely \Doctrine\DBAL\Logging\SQLLogger()
https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Logging/SQLLogger.php
$config = new Doctrine\ORM\Configuration ();
// ... config stuff
$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
$connectionParams = array(
'dbname' => 'example',
'user' => 'example',
'password' => 'example',
'host' => 'localhost',
'driver' => 'pdo_mysql');
//make the connection through an Array of params ($connectionParams)
$em = EntityManager::create($connectionParams, $config);
While i'm creating basic page in drupal i select text format to PHP code. Now I can write PHP code here.
I want to list records from MySQL, How can I include a database-connection here?
Is it good way writing PHP code in content?
you can directly call drupal api functions like db_query() without adding any dbconection in a page. Adding php codes inside a page is not recommented, try to create your own module for that or use views
First: you should not use the PHP-input format. The fact that it is there is highly debated in Drupal, with many people leaning towards removing it entirely. It is both a security nightmare and a maintanance-horror.
That said, the PHP you are looking for is:
<?php
// Create an object of type SelectQuery
$query = db_select('users', 'u');
// Add extra detail to this query object: a condition, fields and a range
$query->condition('u.uid', 0, '<>');
$query->fields('u', array('uid', 'name', 'status', 'created', 'access'));
$query->range(0, 50);
?>
More on dynamic queries is found on Drupal.org. No need to set-up your database, Drupal already handled that.
In case you want your code to connect to a differnt database then the main Drupal-database, you can either add that database to your settings.php:
$databases['gallery']['gallery'] = array(
'driver' => 'mysql',
'database' => 'gallery',
'username' => 'username',
'password' => 'secret',
'host' => 'localhost',
);
This introduces a new database resource called "gallery" using a database "gallery". Useful when you want to use resources from another database, like records from a "gallery application".
You can then query that database using the key in the $options-parameter, like so:
<?php
// Create an object of type SelectQuery
$query = db_select('pictures', 'p', array('target' => 'gallery'));
// Add extra detail to this query object: a condition, fields and a range
$query->condition('p.status', 'published', '=');
$query->fields('p', array('path', 'title', 'date'));
$query->range(0, 50);
?>
In my form, I want to set the selected (default) value for a select element. However, using setDefaults is not working for me.
Here is my code:
$gender = new Zend_Form_Element_Select('sltGender');
$gender->setMultiOptions(array(
-1 => 'Gender',
0 => 'Female',
1 => 'Male'
))
->addValidator(new Zend_Validate_Int(), false)
->addValidator(new Zend_Validate_GreaterThan(-1), false);
$this->setDefaults(array(
'sltGender' => 0
));
$this->addElement($gender);
My controller is simply assigning the form to a view variable which just displays the form.
It works by using $gender->setValue(0), but it would be easier to set them all at once with an array of default values. Am I misunderstanding something here?
Also, where is the Zend Framework documentation for classes and methods? I am looking for something similar to the Java documentation. The best I could find is this one, but I don't like it - especially because every time I try to search, it crashes.
Have you tried:
$this->addElement($gender);
$this->setDefaults(array(
'sltGender' => 0
));
Also, take a look at http://framework.zend.com/issues/browse/ZF-12021 .
As you can see, the above issue is similar to the issue you're describing. It seems Zend is very particular about the order you create objects and assign settings.
I'm afraid you're going to have to do things in the order Zend wants you to do them (which doesn't seem well documented, but is only discovered thru trial and error), or hack their library to make it do what you want it to do.
I'm new to CakePHP and I'm stuck in reading a Model using other fields. I did a cake bake command to generate a simple users CRUD. I can view the user using the url CakePHP provided.
/users/view/1
I can view the user using id = 1. What if I want to view a user by name instead of id?
/users/view/username
By default the view function reads the User model by id.
$this->User->read(null, $id)
Thank you.
you can use find function or findBy<Field>() in your case findByUsername()
check this
I've never used cakePHP myself but I'm going to suggest that you will likely have to implement a new user model method, something like getUserByUsername($username)
This would then in turn interface with your DAL that would get the details of that user based on the username and return a user object that can be used however you wish...
It seems that CakePHP is focusing to deprecate some functions, such as findAll(). Perhaps soon the magic methods such as findBy<field>() will have the same fate.
I can recommend what martswite is suggesting, you should create your custom function:
function findUser($username=''){
return $this->find('first', array(
'conditions' => array(
'User.username' => $username
)
));
}
Perhaps you have a status field, maybe the profile isn't public, you can add a condition:
function findUser($username=''){
return $this->find('first', array(
'conditions' => array(
'User.username' => $username,
'User.status' => 1
)
));
}
I think that's more modular than findBy<Field>.