I want to do a simple join using zf2 selecting fields from the first and second table, but I get an error ('Statement could not be executed') when I try to put an array of fields into the columns method.
public function fetchAll()
{
$resultSet = $this->tableGateway->select(function (Select $select) {
$select->columns();// ->with params gives an 'Statement could not be executed'
$select->join(array('t2' => 'categories'), 'table1.idCategory = t2.id');
$select->order('dateTime DESC')->limit(100);
});
...
}
You will need to make sure the dateTime/id fields are in the column list you provide.
If you dump out the exception ($e->getTraceAsString()) you will get some more information about what is causing your error.
Either comment this statement Or add atleast one valid 'column' name in array (table column/field name).
$select->columns(array('column_one', 'column_two', 'column_N'));
A blank $select->columns(); statement generates sql query something like SELECT FROM table_name which results in error.
When you comment this statement, it generates sql query like SELECT * FROM table_name
and if you pass column names, asterisk replaced by them.
Related
I want to delete multiple rows by TableGateway like below SQL
Delete from table where id in (1,2,5,6) - CSV of multiple ids.
See documentation at https://docs.zendframework.com/zend-db/table-gateway/
Build a Where object to pass as parameter :
$where = new Where();
$where->in('id', [1,2,5,6]);
Scenario:
An alert belongs to 1 user and 1 location, both referenced, respectively, by foreign keys in the alert table - user_id and location_id. The user_id will be the same for each request, but the location_id most definitely differs.
I want to display all alerts relating to that one user, I have successfully achieved this but without the use of eager loading.
my getIndex function so far:
public function getIndex()
{
$alert = User::with('alerts.location')
->where('id', '=', Auth::user()->id)->first();
$this->layout->content = View::make('agents.index',
array('alert' => $alert));
}
Printing the MYSQL query seems logically correct however, I am struggling to show the 'locations' part of the query.
My foreach loop is:
#foreach($alert->locations as $alert)
<td>{{ $alert->location->address_1}}</td>
#endforeach
However, it returns the error:
Invalid argument supplied for foreach()
Thank you for your help.
By accessing the data using this forloop, I was able to resolve the problem #foreach($alert->alerts as $alert).
I have a "comments" String field in my domain. On each save or update of the field I want to check if the field contains a sub string that matches a String from another field of the same domain (or different domain for that matter). I need to run through all the instances of that field to see if there is a match. If it matches I want to transform it into a link inside the comments field to a show action for that entry matching the sub string.
So for example, a comments field for a product with a serial number would note if the product has been replaced by another one by giving it's serial number in the comments field. Like: "This product was replaced by SN1234". I want to automatically transform SN1234 into a link to show the product with serial number SN1234.
What is the best way to go about this ? In the controller, in the GSP ? How ?
As long as the column you're trying to match on is indexed, you'll just need to do a query for the match and if found, modify your comment to include the URL. Controller or Service doesn't really matter for the lookup (although I would probably put it in a service). You'll want to be sure the search is not transactional so it will be as fast as it can be. No way I would do any of this in a GSP.
To insert the link, you could a simple find and replace. Once you know which text you want turned into the link, pseudo code follows:
def comment = "This product was replaced by SN1234"
def match = "SN1234"
def link = g.link(action: "show", controller: "product", id: "${product.id}", match)
comment = comment.replace(match, link)
Which you would then end up with
"This product was replaced by <a href='/product/1234'>SN1234</a>"
There may be more efficient ways to do this, but this is a good place to start.
You can use GORM events to do it in your domain. So whenever the domain is inserted/updated you can check that your field has been changed. Then you can insert your link.
def beforeInsert() {
yourMethod()
}
def beforeUpdate() {
if (isDirty('yourField')) {
yourMethod()
}
}
I've extended the pages table and now I want to use some of the data in a domain object called "Tags".
So I tried the following in the /Configuration/TypoScript/setup.txt:
plugin.myextension.persistence.classes.Tx_myextension_Domain_Model_Tag {
mapping {
tableName = pages
recordType = Tx_myextension_Domain_Model_Tag
columns {
tx_myextension_tag_name.mapOnProperty = name
uid.mapOnProperty = id
}
}
}
But It seems that the extension tries to access the table Tx_myextension_Domain_Model_Tag (which doesn't exist)
This is the error I receive:
Tx_Extbase_Persistence_Storage_Exception_SqlError`
Table 'tx_myextension_domain_model_tag' doesn't exist: SELECT tx_myextension_domain_model_tag.* FROM tx_myextension_domain_model_tag WHERE tx_myextension_domain_model_tag.id = '24' LIMIT 1
What have I done wrong?
Don't forget to include your extension typoscript template into your template ( template > edit whole template > include static templates ), otherwise your setup.txt is not evaluated.
To check which recordType(s) are acceptable use Configuration module in BE, in $TCA section find your table ([pages] in this case) and check type column (...[ctrl][type] - for pages it's 'doktype', which decides if page record is standard page or sysfolder etc.).
This column is tinyint(3) in database, so you can not write value 'Tx_myextension_Domain_Model_Tag' to it. Create in your ext new doktype identified by number and set recordType to it.
Optionaly you can just remove recordType from mapping config if page's type doesn't matter to you.
did you try "config.tx_extbase" instead of "plugin.myextension"?
Something like
config.tx_extbase.persistence.classes.Tx_MyExtension_Domain_Model_Tag.mapping.tableName = pages
works for me.
Tx_myextension_Domain_Model_Tag is the name of your object right ? But I guess this is not the name of the table you are trying to access. So my guess is that the name you are providing into the value "tableName" is wrong. What does "pages" contain ?
Have you specified the individual pages as recordtype Tx_myextension_Domain_Model_Tag ?
It's supposed to go into the doctype field of the pages table (therefore you'll need to change the mysql datatype of that field.
Otherwise Extbase doesn't know that this specific page is an extbase record and not a regular page.
See more about single table inheritance (STI) in Extbase: http://pascal-jungblut.com/blog/blog-post/2010/11/06/single-table-inheritance-in-extbase.html
do you do this in a typo3 call with eID?
here some configuration is not loaded ..
if yes, try if loading all configuration solves the problem:
...
$GLOBALS['TSFE'] = \t3lib_div::makeInstance('tslib_fe', $TYPO3_CONF_VARS, $_GET["id"], 0, true);
//$GLOBALS['TSFE'] = new $temp_TSFEclassName();
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->getCompressedTCarray();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
...
Take care of the proper naming convention regarding FE-Plugins:
plugin.tx_myextension
I have 1 to many relationship with the following tables - Person and Email. When using linq to sql and ASP.Net MVC, I'd like to show the first email or an empty string in my Person view using code like this:
<%= Html.Encode(Model.Emails.FirstOrDefault().EmailAddress) %>
In cases where there are no email rows, I receive a NullReferenceException. I can return null safe values from SQL by using a view or sproc, but I'd like to just stick with generic linq to sql objects bound to tables.
Model.Emails.Select(x => x.EmailAddress).FirstOrDefault() ?? string.Empty
<%= Html.Encode((Model.Emails.FirstOrDefault() ?? new Email { EmailAddress = string.Empty }).EmailAddress) %>
Would work, not super clean to read though.
My vote for:
Model.Emails.Select(z => z.EmailAddress).DefaultIfEmpty("zzz").FirstOrDefault();
I thought that you could do it all inside the FirstOrDefault, but I was wrong-o! However, I also forgot that when you use DefaultIfEmpty you can just call First().
Model.Emails.Select(z => z.EmailAddress).DefaultIfEmpty("zzz").First();
Of course, replace ZZZ with just "" (not string.empty, that is unnecessary), but it is nice to see those records where the default is being chosen explicity when you are first writing it.