I was recently trying to use the cfscript version of cfcomponent along with JavaDoc notation and getting error:
The system has attempted to use an undefined value, which usually
indicates a programming error, either in your code or some system code.
Null Pointers are another name for undefined values.
From CF docs:
/**
* custom metadata for a cfc defined using annotation as well as key-value pairs
* #cfcMetadata1 "cfc metadata1"
*/
component cfcMetadata2 = "cfc metadata2"
{
/**
* custom metadata for a property defined using annotation as well as key-value pairs
* #propMetadata1 "property metadata1"
*/
property type="numeric" name="age" default="10" propMetadata2="property metadata2";
/**
* custom metadata for a function/argument using both annotation and key-value pairs
* #arg1.argmdata1 "arg metadata1"
* output true
* #fnMetadata1 "function metadata1"
*/
public string function foo(required numeric arg1=20 argmdata2="arg metadata2") fnMetadata2="function metadata2"
{
writedump(getmetadata(this));
return arg1;
}
}
My Code:
/**
* #displayName Test
* #output false
*
* #since 2016-10-25
* #version 1.0
*/
component {
/**
* I am a test function.
*
* #limitFrom.required false
* #limitFrom.default 0
*
* #limitBy.required false
* #limitBy.default 0
*
* #returnFormat "json"
*
* #since 2016-10-25
* #version 1.0
*/
remote query function test(
numeric limitFrom,
numeric limitBy
) {
return queryNew("");
}
}
This is only happening when I am trying to assign default value to the arguments using the JavaDoc notation i.e.,
#limitFrom.default 0
#limitBy.default 0
Removing these and everything is fine. Not sure why this is happening?
I know you specifically asked about JavaDoc notation, but I'm not a fan, and it's possible that you haven't seen any alternative. I prefer this syntax.
component
displayName="test"
output="false"
hint="blah blah description..."
{
// beware of `default`--it doesn't do what you might think.
// it's ignored unless you use ORM
property type="numeric" name="age" default="10"
hint="blah blah description..."
propMetadata2="property metadata2";
remote query function test(
numeric limitFrom=0 hint="blah blah description...",
numeric limitBy=0 hint="blah blah description..."
)
hint="I am a test function"
{
return queryNew("");
}
}
Related
Yep, the title suggests: Doctrine is looking for a fieldname that's not there. That's both true and not true at the same time, though I cannot figure out how to fix it.
The full error:
File: D:\path\to\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\AbstractMySQLDriver.php:71
Message: An exception occurred while executing 'SELECT DISTINCT id_2
FROM (SELECT p0_.name AS name_0, p0_.code AS code_1, p0_.id AS id_2
FROM product_statuses p0_) dctrn_result ORDER BY p0_.language_id ASC, name_0 ASC LIMIT 25
OFFSET 0':
SQLSTATE[42S22]: Column not found: 1054 Unknown column
'p0_.language_id' in 'order clause'
The query the error is caused by (from error above):
SELECT DISTINCT id_2
FROM (
SELECT p0_.name AS name_0, p0_.code AS code_1, p0_.id AS id_2
FROM product_statuses p0_
) dctrn_result
ORDER BY p0_.language_id ASC, name_0 ASC
LIMIT 25 OFFSET 0
Clearly, that query is not going to work. The ORDER BY should be in the sub-query, or else it should replace p0_ in the ORDER BY with dctrn_result and also get the language_id column in the sub-query to be returned.
The query is build using the QueryBuilder in the indexAction of a Controller in Zend Framework. All is very normal and the same function works perfectly fine when using a the addOrderBy() function for a single ORDER BY statement. In this instance I wish to use 2, first by language, then by name. But the above happens.
If someone knows a full solution to this (or maybe it's a bug?), that would be nice. Else a hint in the right direction to help me solve this issue would be greatly appreciated.
Below additional information - Entity and indexAction()
ProductStatus.php - Entity - Note the presence of language_id column
/**
* #ORM\Table(name="product_statuses")
* #ORM\Entity(repositoryClass="Hzw\Product\Repository\ProductStatusRepository")
*/
class ProductStatus extends AbstractEntity
{
/**
* #var string
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
protected $name;
/**
* #var string
* #ORM\Column(name="code", type="string", length=255, nullable=false)
*/
protected $code;
/**
* #var Language
* #ORM\ManyToOne(targetEntity="Hzw\Country\Entity\Language")
* #ORM\JoinColumn(name="language_id", referencedColumnName="id")
*/
protected $language;
/**
* #var ArrayCollection|Product[]
* #ORM\OneToMany(targetEntity="Hzw\Product\Entity\Product", mappedBy="status")
*/
protected $products;
[Getters/Setters]
}
IndexAction - Removed parts not directly related to QueryBuilder. Added in comments showing params as they are.
/** #var QueryBuilder $qb */
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select($asParam) // 'pro'
->from($emEntity, $asParam); // Hzw\Product\Entity\ProductStatus, 'pro'
if (count($queryParams) > 0 && !is_null($query)) {
// [...] creates WHERE statement, unused in this instance
}
if (isset($orderBy)) {
if (is_array($orderBy)) {
// !!! This else is executed !!! <-----
if (is_array($orderDirection)) { // 'ASC'
// [...] other code
} else {
// $orderBy = ['language', 'name'], $orderDirection = 'ASC'
foreach ($orderBy as $orderParam) {
$qb->addOrderBy($asParam . '.' . $orderParam, $orderDirection);
}
}
} else {
// This works fine. A single $orderBy with a single $orderDirection
$qb->addOrderBy($asParam . '.' . $orderBy, $orderDirection);
}
}
================================================
UPDATE: I found the problem
The above issue is not caused by incorrect mapping or a possible bug. It's that the QueryBuilder does not automatically handle associations between entities when creating queries.
My expectation was that when an entity, such as ProductStatus above, contains the id's of the relation (i.e. language_id column), that it would be possible to use those properties in the QueryBuilder without issues.
Please see my own answer below how I fixed my functionality to be able to have a default handling of a single level of nesting (i.e. ProducStatus#language == Language, be able to use language.name as ORDER BY identifier).
Ok, after more searching around and digging into how and where this goes wrong, I found out that Doctrine does not handle relation type properties of entities during the generation of queries; or maybe does not default to using say, the primary key of an entity if nothing is specified.
In the use case of my question above, the language property is of a #ORM\ManyToOne association to the Language entity.
My use case calls for the ability to handle at lease one level of relations for default actions. So after I realized that this is not handled automatically (or with modifications such as language.id or language.name as identifiers) I decided to write a little function for it.
/**
* Adds order by parameters to QueryBuilder.
*
* Supports single level nesting of associations. For example:
*
* Entity Product
* product#name
* product#language.name
*
* Language being associated entity, but must be ordered by name.
*
* #param QueryBuilder $qb
* #param string $tableKey - short alias (e.g. 'tab' with 'table AS tab') used for the starting table
* #param string|array $orderBy - string for single orderBy, array for multiple
* #param string|array $orderDirection - string for single orderDirection (ASC default), array for multiple. Must be same count as $orderBy.
*/
public function createOrderBy(QueryBuilder $qb, $tableKey, $orderBy, $orderDirection = 'ASC')
{
if (!is_array($orderBy)) {
$orderBy = [$orderBy];
}
if (!is_array($orderDirection)) {
$orderDirection = [$orderDirection];
}
// $orderDirection is an array. We check if it's of equal length with $orderBy, else throw an error.
if (count($orderBy) !== count($orderDirection)) {
throw new \InvalidArgumentException(
$this->getTranslator()->translate(
'If you specify both OrderBy and OrderDirection as arrays, they should be of equal length.'
)
);
}
$queryKeys = [$tableKey];
foreach ($orderBy as $key => $orderParam) {
if (strpos($orderParam, '.')) {
if (substr_count($orderParam, '.') === 1) {
list($entity, $property) = explode('.', $orderParam);
$shortName = strtolower(substr($entity, 0, 3)); // Might not be unique...
$shortKey = $shortName . '_' . (count($queryKeys) + 1); // Now it's unique, use $shortKey when continuing
$queryKeys[] = $shortKey;
$shortName = strtolower(substr($entity, 0, 3));
$qb->join($tableKey . '.' . $entity, $shortName, Join::WITH);
$qb->addOrderBy($shortName . '.' . $property, $orderDirection[$key]);
} else {
throw new \InvalidArgumentException(
$this->getTranslator()->translate(
'Only single join statements are supported. Please write a custom function for deeper nesting.'
)
);
}
} else {
$qb->addOrderBy($tableKey . '.' . $orderParam, $orderDirection[$key]);
}
}
}
It by no means supports everything the QueryBuilder offers and is definitely not a final solution. But it gives a starting point and solid "default functionality" for an abstract function.
I currently have a fairly complex native SQL query which is used for reporting purposes. Given the amount of data it processes this is the only efficient way to handle it is with native SQL.
This works fine and returns an array of arrays from the scalar results.
What I'd like to do, to keep the results consistent with every other result set in the project is use a Data Transfer Object (DTO). Returning an array of simple DTO objects.
These work really well with DQL but I can't see anyway of using them with native SQL. Is this at all possible?
Doctrine can map the results of a raw SQL query to an entity, as shown here:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/native-sql.html
I cannot see support for DTOs unless you are willing to use DQL as well, so a direct solution does not exist. I tried my hand at a simple workaround that works well enough, so here are the DQL and non-DQL ways to achieve your goal.
The examples were built using Laravel and the Laravel Doctrine extension.
The DTO
The below DTO supports both DQL binding and custom mapping so the constructor must be able to work with and without parameters.
<?php namespace App\Dto;
/**
* Date with corresponding statistics for the date.
*/
class DateTotal
{
public $taskLogDate;
public $totalHours;
/**
* DateTotal constructor.
*
* #param $taskLogDate The date for which to return totals
* #param $totalHours The total hours worked on the given date
*/
public function __construct($taskLogDate = null, $totalHours = null)
{
$this->taskLogDate = $taskLogDate;
$this->totalHours = $totalHours;
}
}
Using DQL to fetch results
Here is the standard version, using DQL.
public function findRecentDateTotals($taskId)
{
$fromDate = new DateTime('6 days ago');
$fromDate->setTime(0, 0, 0);
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->select('NEW App\Dto\DateTotal(taskLog.taskLogDate, SUM(taskLog.taskLogHours))')
->from('App\Entities\TaskLog', 'taskLog')
->where($queryBuilder->expr()->orX(
$queryBuilder->expr()->eq('taskLog.taskLogTask', ':taskId'),
$queryBuilder->expr()->eq(0, ':taskId')
))
->andWhere(
$queryBuilder->expr()->gt('taskLog.taskLogDate', ':fromDate')
)
->groupBy('taskLog.taskLogDate')
->orderBy('taskLog.taskLogDate', 'DESC')
->setParameter(':fromDate', $fromDate)
->setParameter(':taskId', $taskId);
$result = $queryBuilder->getQuery()->getResult();
return $result;
}
Support for DTOs with native SQL
Here is a simple helper that can marshal the array results of a raw SQL query into objects. It can be extended to do other stuff as well, perhaps custom updates and so on.
<?php namespace App\Dto;
use Doctrine\ORM\EntityManager;
/**
* Helper class to run raw SQL.
*
* #package App\Dto
*/
class RawSql
{
/**
* Run a raw SQL query.
*
* #param string $sql The raw SQL
* #param array $parameters Array of parameter names mapped to values
* #param string $className The class to pack the results into
* #return Object[] Array of objects mapped from the array results
* #throws \Doctrine\DBAL\DBALException
*/
public static function query($sql, $parameters, $className)
{
/** #var EntityManager $em */
$em = app('em');
$statement = $em->getConnection()->prepare($sql);
$statement->execute($parameters);
$results = $statement->fetchAll();
$return = array();
foreach ($results as $result) {
$resultObject = new $className();
foreach ($result as $key => $value) {
$resultObject->$key = $value;
}
$return[] = $resultObject;
}
return $return;
}
}
Running the raw SQL version
The function is used and called in the same way as other repository methods, and just calls on the above helper to automate the conversion of data to objects.
public function findRecentDateTotals2($taskId)
{
$fromDate = new DateTime('6 days ago');
$sql = "
SELECT
task_log.task_log_date AS taskLogDate,
SUM(task_log.task_log_hours) AS totalHours
FROM task_log task_log
WHERE (task_log.task_log_task = :taskId OR :taskId = 0) AND task_log.task_log_date > :fromDate
GROUP BY task_log_date
ORDER BY task_log_date DESC
";
$return = RawSql::query(
$sql,
array(
'taskId' => $taskId,
'fromDate' => $fromDate->format('Y-m-d')
),
DateTotal::class
);
return $return;
}
Notes
I would not dismiss DQL too quickly as it can perform most kinds of SQL. I have however also recently been involved in building management reports, and in the world of management information the SQL queries can be as large as whole PHP files. In that case I would join you and abandon Doctrine (or any other ORM) as well.
I am using phppowerpoint to generate ppt files. I want to change default line height between lines in PPT.
here is bit of code I am using to generate ppt.
$shape->createParagraph()->getAlignment()->setHorizontal(PHPPowerPoint_Style_Alignment::HORIZONTAL_LEFT)->setVertical(PHPPowerPoint_Style_Alignment::VERTICAL_TOP);
$textRun = $shape->createTextRun($ppt_build[$i][$k]['text'][$j]['textrun']);
$textRun->getFont()->setBold($ppt_build[$i][$k]['text'][$j]['bold']);
$textRun->getFont()->setSize($ppt_build[$i][$k]['text'][$j]['size']); //setName
$textRun->getFont()->setName($ppt_build[$i][$k]['text'][$j]['name']); //setName
$textRun->getFont()->setColor(new PHPPowerPoint_Style_Color($ppt_build[$i][$k]['text'][$j]['color']));
then creating a line break
$shape->createBreak();
I tried using giving 2 line breaks but it is more than I need.
I also tried solution mentioned here
http://phppowerpoint.codeplex.com/discussions/273396
But with that solution it adds space only before and after the paragraph.
is there any cheat sheet or list that contains xml nodes/elements/names used in xml files generated for powerpoint
follow below steps to achieve this
Inside
/* /powerpoint/PHPPowerPoint/Shape/RichText/Paragraph.php */
private $_spacing;
also
/**
* Get spacing
*
* #return PHPPowerPoint_Style_Spacing
*/
public function getSpacing() {
return $this->_spacing;
}
/**
* Set spacing
*
* #param PHPPowerPoint_Style_Spacing $spacing
* #throws Exception
* #return PHPPowerPoint_Shape_RichText_Paragraph
*/
public function setSpacing($spacing = '100000') {
$this->_spacing = $spacing;
return $this;
}
Then inside /PHPPowerPoint/Writer/PowerPoint2007/Slide.php
private function _writeParagraphs(PHPPowerPoint_Shared_XMLWriter $objWriter, $paragraphs) {
after
$objWriter->writeAttribute('lvl', $paragraph->getAlignment()->getLevel());
add
if ($paragraph->getSpacing()) {
$objWriter->startElement('a:lnSpc');
$objWriter->startElement('a:spcPct');
$objWriter->writeAttribute('val', $paragraph->getSpacing());
$objWriter->endElement();
$objWriter->endElement();
}
I want to do a DQL query like:
$dql = "select p
from AcmeDemoBundle:UserTypeA p
where p.UserTypeB = :id
and (
select top 1 r.boolean
from AcmeDemoBundle:Registry r
)
= true";
But it seems that TOP 1 it's not a valid function in doctrine2.
I can't figure out how can I limit the result of the subquery to one row.
DQL does not support limits on subqueries and neither LIMIT nor OFFSET.
See http://www.doctrine-project.org/jira/browse/DDC-885
Although Doctrine doesn't natively support this, you could implement a custom function named FIRST() to achieve this:
<?php
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\Subselect;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* FirstFunction ::=
* "FIRST" "(" Subselect ")"
*/
class FirstFunction extends FunctionNode
{
/**
* #var Subselect
*/
private $subselect;
/**
* {#inheritdoc}
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->subselect = $parser->Subselect();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
/**
* {#inheritdoc}
*/
public function getSql(SqlWalker $sqlWalker)
{
return '(' . $this->subselect->dispatch($sqlWalker) . ' LIMIT 1)';
}
}
(More details: https://www.colinodell.com/blog/201703/limiting-subqueries-doctrine-2-dql)
You should really only use this for read-only purposes since Doctrine will not include other related entities in the result (which may become orphaned or lost if you save it).
I started playing with the new cfproperty stuff added in ColdFusion 9, but the primary piece that I want to use doesn't seem to work now in ColdFusion 10. I created the following CFC:
component displayName="Sources" {
/**
* #getter true
* #setter true
* #type numeric
* #default 1
**/
property sourceid;
/**
* #getter true
* #setter true
* #type numeric
* #default 1
**/
property sourcegroup;
public any function init () {
This.domainRegex = '\/\/(www\.)?(([A-Za-z0-9\-_]+\.?)+)';
return this;
}
}
When I dump the meta data for the CFC I can see the properties, but no methods created for them and I can't call getSourceId() or getSourceGroup()
try this:
component accessors="true" displayName="Sources" {
property name="sourceid" type="numeric" default="1";
property name="sourcegroup" type="numeric" default="1";
public any function init () {
this.domainRegex = '\/\/(www\.)?(([A-Za-z0-9\-_]+\.?)+)';
return this;
}
}
Try removing the second star in the closing comment, the CF examples all only have one.
Alternatively, use the other syntax:
property name="sourceid" type="numeric" default="1";
I'm not a fan of annotations in comments for anything other JavaDoc, it just doesn't feel right somehow.