I need help on this, I have a model
/**
* This is the model class for table "student_attachment_details".
*
* #property integer $createdBy
* #property string $reg_no
* #property string $county_attached
* #property string $closest_town
* #property string $company_attached
* #property integer $company_phone_number
* #property string $is_assessed
* #property string $location_description
* #property integer $department_id
* #property string $allocated_staff_id
*/
class StudentAttachmentDetails extends \yii\db\ActiveRecord
{
But when i try to query the model like this
$student_details = StudentAttachmentDetails::find()
->where(['allocated_staff_id'=>'no'])
->limit(1);
And then try to get a value with this
$reg_no = $student_details->reg_no;
I get this error
Getting unknown property: yii\db\ActiveQuery::reg_no
What am I doing wrong?
the method ActiveRecord::find() returns an ActiveQuery; to access the actual model (in your case of class StudentAttachmentDetails) you need to call a method to execute the ActiveQuery.
something like
$student_details = StudentAttachmentDetails::find()
->where(['allocated_staff_id'=>'no'])
->limit(1)->one();
will give you the record you are looking for. The docs for ActiveRecord::find() can be found here and ActiveQuery::one() here.
Related
I have a bunch of fields, like
/**
* #var string
* Some stuff here
*/
private $var3;
/**
* #var int
* Some other stuff
*/
private $var2;
// Just a comment
private $var1;
I'd like to have them all sorted alphabetically. The end result should be
// Just a comment
private $var1;
/**
* #var int
* Some other stuff
*/
private $var2;
/**
* #var string
* Some stuff here
*/
private $var3;
I tried using a plugin called "String manipulation", but it doesn't keep the comments together with the fields, sorting resulting in something random like this
* #var int
* #var string
* Some other stuff
* Some stuff here
*/
*/
/**
/**
// Just a comment
private $var1;
private $var2;
private $var3;
I also tried to use the Rearrange code built-in the IDE. I kept the default rules, but changed the "fields" one to "Order by name". Doesn't seem to do anything...
I don't have any other ideas on what to try, and with many large files, doing this manually is a tedious work.
Any solutions?
On my Symfony 5 app, i've a database with a candidate table that contains a json field.
candidate 1 : [{"end": "30/04/2020", "start": "01/03/2020"},{"end": "31/07/2020", "start": "01/07/2020"}]
candidate 2 : [{"end": "31/03/2020", "start": "01/03/2020"},{"end": "31/07/2020", "start": "01/07/2020"}]
Is it possible with query builder to find a candidate where this field corresponds to the arguments ?
ex: I would like to find all the candidates who are available between 10/03/2020 and 10/04/2020.
This case should just return the candidate 1.
I guess it's not possible to do this with query builder so i'm trying to use native SQL but... what's the sql syntax ?
I tried with availability_dates`->"$.start" = "01/03/2020" but it does not work because it's a "collection".
This is a poorly-conceived database structure. Clearly, the JSON string represents a "repeating group" of related data, which violates the principles of so-called "normal forms."
https://en.wikipedia.org/wiki/Database_normalization
You should be storing the start/end dates in a separate table, say, candidate_dates, with columns like candidate_id, start, end. This has a so-called "one-to-many relationship" to the parent table, candidates.
Now, you can write a simple query which JOINs the two tables to get the answers you need.
Entity like that ?Entity like that ?
One candidate can have one or more available dates and one available dates can only be linked to one candidate.
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Table(name="candidate_available_dates", uniqueConstraints={
* #ORM\UniqueConstraint(name="unique_candidate_available_dates", columns={"candidate_id", "start", "end"})
* })
*
* #ORM\Entity(repositoryClass="App\Repository\CandidateAvailableDatesRepository")
*/
class CandidateAvailableDates
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Candidate", inversedBy="candidateAvailableDates")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="candidate_id", referencedColumnName="candidate_id", nullable=false)
* })
*/
private $candidate;
/**
* #ORM\Column(type="date")
* #Assert\NotBlank
*/
private $start;
/**
* #ORM\Column(type="date")
* #Assert\NotBlank
*/
private $end;
[...]
// GETTER and SETTER
And in Candidate entity, the reversed side
/**
* #ORM\OneToMany(targetEntity="App\Entity\CandidateAvailableDates", mappedBy="candidate")
*/
private $candidateAvailableDates;
I have tried to make dropdown list but why its just empety result?
This is the way i made the drop down
$form->field($model, 'ID_DATA_PROPERTIES')->dropDownList($list_properties, ['prompt'=>'-Choose a Course-'])
This app/models/Request :
i have table request
* #property integer $ID_REQUEST
* #property integer $ID_DATA_PROPERTIES //foreignkeys
//request related to data_properties
* #property DataProperties $iDDATAPROPERTIES
/**
* #return \yii\db\ActiveQuery
*/
public function getIDDATAPROPERTIES()
{
return $this->hasOne(DataProperties::className(), ['ID_DATA_PROPERTIES' => 'ID_DATA_PROPERTIES']);
}
The table data_properties : ID_DATA_PROPERTIES, NAMA_DATA_PROPERTIES
The controller :
$model = new Requestdata();
$list_properties = ArrayHelper::map(Requestdata::find()->all(), 'iDDATAPROPERTIES', 'NAMA_DATA_PROPERTIES');
i found the answer, i was put wrong model.
i made dataproperties model by gii. and use it for find data list i need
$list_properties = ArrayHelper::map(DataProperties::find()->all(), 'ID_DATA_PROPERTIES', 'NAMA_DATA_PROPERTIES');
$rootNode
->children()
->arrayNode('form')
->info('form configuration')
->canBeUnset()
->treatNullLike(array('enabled' => true))
->treatTrueLike(array('enabled' => true))
->children()
->booleanNode('enabled')->defaultTrue()->end()
->end()
->end()
Line 5 of the above snippet from Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration uses the method canBeUnset(). I don't know what this does because it seems to not do anything if I remove it. I'm working understanding semantic configuration for my own bundles.
Following the code, you can find definition for this method in Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition class.
/**
* Sets whether the node can be unset.
*
* #param Boolean $allow
*
* #return ArrayNodeDefinition
*/
public function canBeUnset($allow = true)
{
$this->merge()->allowUnset($allow);
return $this;
}
This is passed to MergeBuilder ( Symfony/Component/Config/Definition/Builder/MergeBuilder ) which handles config merging.
/**
* Sets whether the node can be unset.
*
* #param Boolean $allow
*
* #return MergeBuilder
*/
public function allowUnset($allow = true)
{
$this->allowFalse = $allow;
return $this;
}
So my understanding is, that this method defines, if your config value can be unset while merging configurations, in case the overriding config does not support the value. I would have to test though, to find out the behaviour if the unsetting is not allowed, but I guess then it would throw an exception about a missing config value just like isRequired.
Ok i have a couple of tables in a database that I CAN NOT change the the structure to. That being said what I am trying to accomplish is to have a controller interact with the entities, do a custom join and return the results.
MORE DETAILS:
1st table has id, username
2nd table has user_id, stat1, stat2, stat3
what i want is to search for all users in table 1 joining table 2. I can do this with straight MySQL pretty easy but i want to know how to do it the symfony way.
You need to tell Doctrine where to find each piece of information so that it can load all the properties every time you instantiate a new User object. In other words, you need to add custom Doctrine mapping information. Assuming you're adding your mapping information as inline annotations to your User's model class, the code would look something like this:
//in User.php
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="first_table_name")
*/
class User
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $username;
/**
* #ORM\OneToMany(targetEntity="UserStats")
*/
protected $stats;
//also define getters and setters for the above, of course
}
//in UserStats.php
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="table_two_name")
*/
class UserStats
{
/**
* I'm pretty sure doctrine will require that you add an Id column to table_two,
* which is what this is. If you can't add an Id, I'm not sure it'll work...
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* The below assumes your stats are strings. If not, change the type attribute.
* #ORM\Column(type="string")
*/
protected $stat1;
/**
* #ORM\Column(type="string")
*/
protected $stat2;
/**
* #ORM\Column(type="string")
*/
protected $stat3;
//include appropriate getters/setters here too
}