For doctrine 1.2, I find plenty of examples like these:
$q = Doctrine_Query::create()
->update('mytable')
->set('amount', 'amount+200')
->whereIn ('id', $ids);
What I want to do however is a different set:
->set('name', 'foo bar')
This however, leads to exception. Of course, because foo is no column, like amount.
No luck with '´foo bar´' either. How can I clarify foo bar is a string literal?
I believe this is the right place to look, but I don't find further info.
Also: I would love to know more about the 'mixed params' and 'string update' mentioned there. Perhaps there's a DOCTRINE::FLAG_LITERAL or such?
Use:
->set('name', '?', 'foo bar')
This is the equivalent of doing the following in DQL
SET name = ?
You can also use :
$q = Doctrine_Query::create()
->update('mytable')
->set('name', ':name')
->setParameter('name', 'foo bar')
Related
How does one indirectly refer to anything? In my case it's a column name. The "Field Access" section of the language doc left me with no clue.
For example
let
OriginalStrings = "some string with {tkn1} and/or {tkn2}"
,ParamTable = [tkn1 = "this", tkn2 = "that"]
,Result = List.Accumulate(Record.FieldNames(ParamTable),OriginalStrings
,(txt,current) => Text.Replace(txt,"{"¤t&"}",ParamTable[current]))
in
Result
This results in: Field 'current' of record not found. What I want is the value of 'current' as an identifier and not the literal.
The code above should result in "some string with this and/or that"
Similar to my answer here, Expression.Evaluate may work for what you're after.
Replace ParamTable[current] with
Expression.Evaluate("ParamTable["¤t&"]", [ParamTable=ParamTable])
Check out the Expression.Evaluate() and non global environments section of this article for more clarity about the environment argument.
I want to make standard request for laravel localization file: animals.php
which looks like:
<?php
return [
'dog' => 'dog trans',
'cat' => 'cat trans',
'super-mutant-spider' => 'super-mutant-spider trans',
];
Now when I'm accessing this, I'm simply writing:
trans('animals.dog') -> gives dog trans etc.
this is fine,
now I want to make it depend on the user variable animal:
so when $user->animal is 'dog' I want dog trans result.
so when I try: trans('animals.$user->animal') it will not work
How can I code it?
This is basic string concatenating.
What you need is basically this: trans('animals.' . $user->animal)
You have detailed explanation on PHP - concatenate or directly insert variables in string
Newbie to Ruby here and I'm trying to figure something out. I've got a situation where I have something like the following:
sql = q%{select foo from bar where var1 = :ugh and var2 = :moreugh}
Now, I can print out variable "sql", but if I just use puts and #{sql} it shows the symbols as above, ":ugh".
I'd like to be able to print out string sql with the values of the symbols displayed rather than the names of the symbols.
Any pointers out there? Many thanks!
your example-code is broken, i assume you want to do a single-quoted-string %q{...}
and that is exactly what you get: one string. so there are no symbols in there, it's just ONE string. similar to '...'
I think, assuming you are using symbols instead of string, You can use following query:
sql = "select foo from bar where var1 = 'ugh' and var2 = 'moreugh'"
So using puts or #{sql} will give you correct output.
Comment in case this doesn't meet your requirement.
You probably want this:
sql = "select foo from bar where var1 = '#{ugh}' and var2 = '#{moreugh}"'
where ugh and moreugh are variables. But, you should do it that way, your system would be open to SQL Injection. Use bind variables in your DB engine instead.
You have two errors. You need to use %Q to enable interpolation, and variables are included in the string in the form #{:ugh}. So, you want:
sql = %Q{select foo from bar where var1 = #{ugh} and var2 = #{moreugh}}
puts sql
But please, please, please don't do this, even just for a small project. You are setting yourself up for a SQL injection attacj.
So here is my problem.
I want to retrieve a string stored in a model and at runtime change a part of it using a variable from the rails application. Here is an example:
I have a Message model, which I use to store several unique messages. So different users have the same message, but I want to be able to show their name in the middle of the message, e.g.,
"Hi #{user.name}, ...."
I tried to store exactly that in the database but it gets escaped before showing in the view or gets interpolated when storing in the database, via the rails console.
Thanks in advance.
I don't see a reason to define custom string helper functions. Ruby offers very nice formatting approaches, e.g.:
"Hello %s" % ['world']
or
"Hello %{subject}" % { subject: 'world' }
Both examples return "Hello world".
If you want
"Hi #{user.name}, ...."
in your database, use single quotes or escape the # with a backslash to keep Ruby from interpolating the #{} stuff right away:
s = 'Hi #{user.name}, ....'
s = "Hi \#{user.name}, ...."
Then, later when you want to do the interpolation you could, if you were daring or trusted yourself, use eval:
s = pull_the_string_from_the_database
msg = eval '"' + s + '"'
Note that you'll have to turn s into a double quoted string in order for the eval to work. This will work but it isn't the nicest approach and leaves you open to all sorts of strange and confusing errors; it should be okay as long as you (or other trusted people) are writing the strings.
I think you'd be better off with a simple micro-templating system, even something as simple as this:
def fill_in(template, data)
template.gsub(/\{\{(\w+)\}\}/) { data[$1.to_sym] }
end
#...
fill_in('Hi {{user_name}}, ....', :user_name => 'Pancakes')
You could use whatever delimiters you wanted of course, I went with {{...}} because I've been using Mustache.js and Handlebars.js lately. This naive implementation has issues (no in-template formatting options, no delimiter escaping, ...) but it might be enough. If your templates get more complicated then maybe String#% or ERB might work better.
one way I can think of doing this is to have templates stored for example:
"hi name"
then have a function in models that just replaces the template tags (name) with the passed arguments.
It can also be User who logged in.
Because this new function will be a part of model, you can use it like just another field of model from anywhere in rails, including the html.erb file.
Hope that helps, let me know if you need more description.
Adding another possible solution using Procs:
#String can be stored in the database
string = "->(user){ 'Hello ' + user.name}"
proc = eval(string)
proc.call(User.find(1)) #=> "Hello Bob"
gsub is very powerful in Ruby.
It takes a hash as a second argument so you can supply it with a whitelist of keys to replace like that:
template = <<~STR
Hello %{user_email}!
You have %{user_voices_count} votes!
Greetings from the system
STR
template.gsub(/%{.*?}/, {
"%{user_email}" => 'schmijos#example.com',
"%{user_voices_count}" => 5,
"%{release_distributable_total}" => 131,
"%{entitlement_value}" => 2,
})
Compared to ERB it's secure. And it doesn't complain about single % and unused or inexistent keys like string interpolation with %(sprintf) does.
Toying with heredocs in PHP, I realized the name of the heredoc does not have to be unique. Thus:
$a = <<<EOD
Some string
EOD;
$b = <<<EOD
A different string
EOD;
is correct and behaves exactly as you would expect.
Is this bad practice for any reason? Why does a heredoc need a name/label (EOD above) at all, since you can't reference it by the name?
What if the string you're specifying contains EOD?
You can select the identifier to avoid conflicts with the chunk of text you're using as a string.
You don't reference it as such, but it acts as an identifier to indicate the end of the heredoc. e.g.
$a = <<<EOD
EOA
EOB
EOC
EOD;
One benefit is that editors like vim can apply syntax highlighting to heredocs named with HTML, EOHTML, EOSQL, EOJAVASCRIPT making them much prettier to work with...
$html = <<<EOHTML
<p class="foo">foo</em>
EOHTML;
$sql = <<<EOSQL
SELECT DISTINCT(name) FROM foo ORDER BY bar;
EOSQL;
$js = <<<EOJAVASCRIPT
foo = { bar: 'bar'; }
EOJAVASCRIPT;