symfony2 createQueryBuilder - symfony

I am trying to make a really simple sql query from repository class, just select * from Adjudicacion where cursoAcademico_id=$cursoAcademicoActual;:
This is my entity:
/**
* Adjudicacion
*
* #ORM\Table(name="Adjudicacion")
* #ORM\Entity(repositoryClass="Administrador\AdjudicacionBundle\Entity\AdjudicacionRepository")
*/
class Adjudicacion {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="fechaInicio", type="date")
*/
private $fechaInicio;
/**
* #var \DateTime
*
* #ORM\Column(name="fechaFinal", type="date")
*/
private $fechaFinal;
/**
* #ORM\ManyToOne(targetEntity="Administrador\CursoAcademicoBundle\Entity\CursoAcademico")
*/
private $cursoAcademico;
/**
* #ORM\ManyToOne(targetEntity="Administrador\AdjudicacionClaseBundle\Entity\AdjudicacionClase")
*/
private $adjudicacionClase;
/**
* #ORM\ManyToOne(targetEntity="Administrador\AdjudicacionNumeroBundle\Entity\AdjudicacionNumero")
*/
private $adjudicacionNumero;
/**
* #ORM\ManyToOne(targetEntity="Administrador\AdjudicacionTipoBundle\Entity\AdjudicacionTipo")
*/
private $adjudicacionTipo;
...getters and setters...
This is my repository class:
class AdjudicacionRepository extends EntityRepository {
public function findAdjudicacionesActuales($cursoAcademicoActual) {
$q=$this->createQueryBuilder('c')
->where('c.cursoAcademico_id = :cursoAcademico_id')
->setParameter('cursoAcademico_id', $cursoAcademicoActual)
->getQuery()->getResult();
return $q;
}
}
But it doesnt work, the screen is just blank and I dont get any result. I tried with criteria too, like this:
public function findAdjudicacionesActuales2($cursoAcademicoActual) {
$expr = Criteria::expr();
$criteria = Criteria::create();
$criteria->where($expr->eq("cursoAcademico_id", $cursoAcademicoActual));
return $this->matching($criteria);
}
and i get: Unrecognized field: cursoAcademico_id
This is in the database:
mysql> select * from Adjudicacion;
+----+-------------+------------+-------------------+----------------------+-----------------------+---------------------+
| id | fechaInicio | fechaFinal | cursoAcademico_id | adjudicacionClase_id | adjudicacionNumero_id | adjudicacionTipo_id |
+----+-------------+------------+-------------------+----------------------+-----------------------+---------------------+
| 2 | 2009-01-01 | 2009-01-01 | 7 | 3 | 4 | 3 |
| 6 | 2009-01-01 | 2009-01-01 | 7 | 3 | 4 | 4 |
| 7 | 2009-01-01 | 2009-01-01 | 7 | 3 | 5 | 3 |
| 8 | 2009-01-01 | 2009-01-01 | 7 | 3 | 5 | 4 |
+----+-------------+------------+-------------------+----------------------+-----------------------+---------------------+
what is wrong?

Technically in the eyes of Doctrine ORM the cursoAcademico_id field doesn't exist. It is used to create a link between the 2 tables to create the object but you can't use it in anything.
To do a search for an object with the given id you should use a join and match the id of the joined object like..
$q=$this->createQueryBuilder('a')
// Create builder in 'Adjudicacion' repository so 'a' rather than 'c'
->join('a.cursoAcademico', 'c')
// Join 'Adjudicacion' to 'CursoAcademico'
->where('c.id = :cursoAcademico_id')
// match id of joined `CursoAcademico`
->setParameter('cursoAcademico_id', $cursoAcademicoActual)
->getQuery()->getResult();
return $q;

Related

Undefined column: 7 ERROR: column ... of relation ... does not exist

A call to the following local endpoint:-
curl -k --location --request POST 'https://localhost:8443/project_exports' \
--header 'Authorization: Bearer eyJ0eXA...YVhA' \
--header 'Content-Type: application/json' \
--data-raw '{
"email": "bob#test.com",
"projectid": 0,
"status": true,
"emailedat": "2022-08-22T09:56:28.487Z",
"generatedat": "2022-08-22T09:56:28.487Z"
}'
returns an error like thus:-
"message": "An exception occurred while executing a query: SQLSTATE[42703]: Undefined column: 7 ERROR: column \"projectid\" of relation \"project_export\" does not exist\nLINE 1: INSERT INTO project_export (id, email, projectid, status, em...\n ^",
"code": 500
But the field exists in both entity and db:-
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\ProjectExportRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* #ORM\Entity(repositoryClass=ProjectExportRepository::class)
*/
#[ApiResource]
class ProjectExport
{
...
/**
* #ORM\Column(type="integer")
*/
#[Groups('entry-index')]
private $projectid;
...
mysql> describe project_export
-> ;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | | NULL | |
| projectid | int | NO | | NULL | |
| status | tinyint(1) | NO | | NULL | |
| emailedat | datetime | NO | | NULL | |
| generatedat | datetime | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
I'm serving the api from a docker instance locally. Not sure if this affects it any way?
So, why then, no data gets persisted and the error thrown?

cron job scheduling timing for excecuting the script

I need to execute a script through cron expression that is 5 days per week (Mon-Fri) between 6pm and 7am GMT.
please advise what will be the cron expression for this..
I have tried this as ...
0 00 23 ? * MON-FRI
as the format is as follows:
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
you may want it to be:
* 6 * * 1-5 command
This will work every minute from 6.00 to 6.59. If you need it to execute also at 7.00:
* 6 * * 1-5 command
0 7 * * 1-5 command
and if you want from 6.00 to 7.59, every minute:
* 6-7 * * 1-5 command

Show joined field data in a select option using Symfony form builder

I am trying to make a form for adding race results to an event, but cant figure out how to display information from joined tables concatenated together in the options of one select box.
Here is my database structure:
Person Table:
id | name
----------
1 | fred
2 | dave
3 | james
Entrant Table (the code is the entrants race number - the kind of thing you pin to your shirt):
id | code | person_id | category_id
---------------------------------
1 | 210 | 1 | 1
2 | 211 | 2 | 1
3 | 212 | 3 | 1
4 | 156 | 1 | 2
6 | 157 | 3 | 2
Results Table:
id | time | entrant_id
---------------------
1 | 1:20 | 1
2 | 1:35 | 2
3 | 2:02 | 3
Desired Output when adding results to Category=1:
<select>
<option>210 - Fred</option>
<option>211 - Dave</option>
<option>212 - James</option>
</select>
The code I have currently got in my ResultType gets me just the person.name which is a good start (at the expense of 1 extra query per name) but not the entrant.code:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('time')
->add('entrant', 'entity', array(
'class'=>'KP\EventsBundle\Entity\Entrant',
'property' => 'person.name',
));
}
How can I get both the entrant.code and the person.name in the same list of options?
I have only been using Symfony for a couple of weeks, so please be gentle with me.
The simplest way I came up with off the top of my head is to implement the __toString() method in the Entrant class:
public function __toString()
{
return sprintf("%d %s", $this->code, $this->person->getName());
}
Remove the property option from entrant field of the form type to make it use the method.
To solve the extra queries problem, you could do a fetch join in the query_builder option of the entity type.

ASCII Plotting Functions for R [duplicate]

This question already has answers here:
How can I generate ascii "graphical output" from R?
(2 answers)
Closed 10 years ago.
In order to access my server, I am forced to work with an old text terminal application that does not have X windows. The best thing I have going is emacs/ESS.
Often, I wish to make a rudimentary plots such as histograms and scatter plots and not have to go through the trouble of transferring the file to a computer with a graphics display.
Is there a text terminal-based R graphics library?
There are a number of things that can do bits of it. There's stem in default R, there's this scatter plot function, but best of all, there's the package txtplot on CRAN which does scatterplots, boxplots, barplots, density traces, acfs and plots curves (like the curve function... kinda).
I only need it once in a while - but if I am trying to convey a rough idea of a graphic in pure text as I sometimes need to, it's a life-saver.
In the past I wrote a short piece of R code that made tally-style ascii graphic in very quick time (like a sideways barchart or a stem-and-leaf plot with the numbers replaced by symbols, which solved a problem I had) - but I didn't keep it since stem mostly covers that territory.
Of course the 'table' facility produces ascii output and can be manipulated to do some interesting/useful semigraphical things.
There's also the package ascii that can be used to render various R objects into ascii form in similar fashion to Sweave - handy for formatting tables and so on. Just formatting a table into ascii is not really what it's for but you might still be able to get some use out of it with a little work and the right output format.
Sample output from txtplot:
scatter plot:
> with(cars,txtplot(speed,dist))
+----+-----------+------------+-----------+-----------+--+
120 + * +
| |
100 + +
| * * |
80 + * * +
| * * * |
60 + * * +
| * * * * * |
40 + * * * * * +
| * * * * * * * |
20 + * * * * * * * +
| * * * * |
| * * * |
0 +----+-----------+------------+-----------+-----------+--+
5 10 15 20 25
acf plot:
> txtacf(ldeaths)
+-+--------------+--------------+--------------+--------+
1 + * +
| * |
| * * * * * |
0.5 + * * * * * +
| * * * * * * * * |
| * * * * * * * * |
| * * * * * * * * |
0 + * * * * * * * * * * * * * * * * * * * * * +
| * * * * * * * * * * |
| * * * * * * * * * * |
| * * * * * * * * * |
-0.5 + * * * * * * +
| * * * * |
+-+--------------+--------------+--------------+--------+
0 0.5 1 1.5
density trace:
> txtdensity(rnorm(100,m=5,s=.1))
+------+----------+----------+----------+----------+-------+
| ***** |
4 + ** *** +
| * *** |
| ** *** |
3 + ** *** +
| *** ** |
| ***** ** |
2 + *** ** +
| *** ** |
| ** ** |
1 + ** *** +
| *** ****** |
| ******** *** |
+------+----------+----------+----------+----------+-------+
4.8 4.9 5 5.1 5.2
box plot:
> vc <- ToothGrowth[,2]=="VC"
> oj <- ToothGrowth[,2]=="OJ"
> txtboxplot(ToothGrowth[vc,1],ToothGrowth[oj,1])
5 10 15 20 25 30 35
|----+-------+--------+--------+--------+--------+-------+--|
+--------+-----------+
1 -------------| | |------------------
+--------+-----------+
+------------+----+
2 -------------| | |---------
+------------+----+
Legend: 1=ToothGrowth[vc, 1], 2=ToothGrowth[oj, 1]
curve plot:
> txtcurve(sin(pi*x),from=0,to=2)
+--+-----------+------------+------------+-----------+--+
1 + ********* +
| *** ** |
| ** ** |
0.5 + ** ** +
| ** ** |
| * ** |
0 + * ** * +
| * * |
| ** ** |
-0.5 + *** ** +
| ** ** |
| ** *** |
-1 + ********* +
+--+-----------+------------+------------+-----------+--+
0 0.5 1 1.5 2
bar chart:
> txtbarchart(as.factor(res),pch="|")
+--+------------+------------+------------+------------+--+
50 + | +
| | |
40 + | +
| | |
30 + | | +
| | | |
| | | |
20 + | | | +
| | | | |
10 + | | | +
| | | | |
0 + | | | +
+--+------------+------------+------------+------------+--+
1 1.5 2 2.5 3
Legend: 1=A, 2=B, 3=C
Add in the stem function from default R graphics:
> stem(log(islands,10))
The decimal point is at the |
1 | 1111112222233444
1 | 5555556666667899999
2 | 3344
2 | 59
3 |
3 | 5678
4 | 012
and you have quite a lot of coverage.

Doctrine 2 schema update produces MySQL errno 150, Foreign Key constraint

When using Doctrine ORM in Symfony2, I have the following tables generated from three different entities, of which accessory has two foreign key constraints (marked A and B below).
describe publication;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| kid | varchar(10) | NO | | NULL | |
| title | varchar(255) | NO | | NULL | |
| title_canonical | varchar(255) | NO | | NULL | | <- A
| created | datetime | NO | | NULL | |
| modified | datetime | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
describe accessory;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| publication_title | varchar(255) | YES | | NULL | | <- A
| index_id | int(11) | NO | | NULL | |
| index_alias | varchar(255) | NO | | NULL | |
| value | longtext | NO | | NULL | |
| attribute_name | varchar(255) | YES | | NULL | | <- B
+-------------------+--------------+------+-----+---------+----------------+
describe attribute;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| name_canonical | varchar(255) | NO | | NULL | | <- B
| parameter | varchar(16) | NO | | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
The foreign keys are mapped with annotations:
Publication.php
/**
* #ORM\OneToMany(targetEntity="Accessory", mappedBy="publication")
*/
protected $accessories;
Accessory.php
/**
* #ORM\ManyToOne(targetEntity="Publication", inversedBy="accessories")
* #ORM\JoinColumn(name="publication_title", referencedColumnName="title_canonical")
*/
protected $publication;
/**
* #ORM\ManyToOne(targetEntity="Attribute", inversedBy="accessories")
* #ORM\JoinColumn(name="attribute_name", referencedColumnName="name_canonical")
*/
protected $attribute;
Attribute.php
/**
* #ORM\OneToMany(targetEntity="Accessory", mappedBy="attribute")
*/
protected $accessories;
but upon running php app/console doctrine:schema:update --force I got this exception
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'ALTER TABLE accessory ADD CONSTRAINT FK_A1B1251CCEE83EE7 FOREIGN KEY (publication_title) REFERENCES publication (title_canonical)':
SQLSTATE[HY000]: General error: 1005 Can't create table 'publicationsapp.#sql-2a3c_2828' (errno: 150)
So I ran php app/console doctrine:schema:update --dump-sql
ALTER TABLE accessory ADD CONSTRAINT FK_A1B1251CCEE83EE7 FOREIGN KEY (publication_title) REFERENCES publication (title_canonical);
ALTER TABLE accessory ADD CONSTRAINT FK_A1B1251C5CBDA8E FOREIGN KEY (attribute_name) REFERENCES attribute (name_canonical);
CREATE INDEX IDX_A1B1251CCEE83EE7 ON accessory (publication_title);
CREATE INDEX IDX_A1B1251C5CBDA8E ON accessory (attribute_name);
What's the correct way to resolve this? Should I edit the tables manually or with Doctrine?
Based on what I've read about errno 150, the foreign column needs to be indexed, but can't Doctrine handle this automatically?
One could add unique=true to both $nameCanonical and $titleCanonical properties or whichever columns are referenced by the foreign key. Then drop and run the schema update command to recreate the tables.
In the case of $titleCanonical
/**
* #var string
*
* #ORM\Column(name="title_canonical", type="string", length=255, unique=true)
*/
private $titleCanonical;
But ideally with Doctrine, the foreign keys should be referenced to the other table's primary key to make it valid.

Resources