i have this schema below, i have generated the classes using symfony
and created a pair of objects using the form class generated.
moto:
marca: { type: varchar(255), required: true }
matricula: { type: integer, required: true }
Now i have this criteria:
$c = new Criteria();
$c->addSelectColumn('MAX('.MotoPeer::MATRICULA.')');
$max_matricula = MotoPeer::doSelect($c);
var_dump($max_matricula);
return $max_matricula;
When i call the criteria code It works ok, but these three notices
below are showed.
Any idea?
sf 1.4/propel 1.4
( ! ) Notice: Undefined offset: 2 in
/opt/lampp/htdocs/prueba/lib/
model/om/BaseMotoPeer.php on line 379
Call Stack
Time Memory Function Location 1 0.0008 328076
{main}( ) ../frontend_dev.php:0
2 0.1974 4333236
sfContext->dispatch( )
../frontend_dev.php:13 3 0.1974
4333264
sfFrontWebController->dispatch( )
../ sfContext.class.php:170 4
0.1981 4350256 sfController->forward( ) ../
sfFrontWebController.class.php:48 5
0.2134 4641000 sfFilterChain->execute( )
../sfController.class.php: 238 6
0.2138 4641808 sfRenderingFilter->execute( )
../ sfFilterChain.class.php:53 7
0.2138 4641808 sfFilterChain->execute( ) ../
sfRenderingFilter.class.php:33 8
0.2143 4642588 sfExecutionFilter->execute( )
../ sfFilterChain.class.php:53 9
0.2144 4643308 sfExecutionFilter->handleAction( )
../ sfExecutionFilter.class.php:42 10
0.2144 4643308 sfExecutionFilter->executeAction( )
../ sfExecutionFilter.class.php:78 11
0.2144 4643336 sfActions->execute( ) ../sfExecutionFilter.class.php: 92 12
0.2147 4644160 motoActions->executePrueba( )
../sfActions.class.php: 60 13
0.2212 5026172 MotoPeer::prueba( ) ../actions.class.php:14 14 0.2254
5285592 BaseMotoPeer::doSelect( )
../MotoPeer.php:26 15 0.2493
5756176 BaseMotoPeer::populateObjects(
) ../BaseMotoPeer.php: 241 16
0.2493 5756568 BaseMotoPeer::getPrimaryKeyHashFromRow(
) ../ BaseMotoPeer.php:400
( ! ) Notice: Undefined offset: 1 in
/opt/lampp/htdocs/prueba/lib/
model/om/BaseMoto.php on line 184 Call
Stack
Time Memory Function Location 1 0.0008 328076
{main}( ) ../frontend_dev.php:0
2 0.1974 4333236
sfContext->dispatch( )
../frontend_dev.php:13 3 0.1974
4333264
sfFrontWebController->dispatch( )
../ sfContext.class.php:170 4
0.1981 4350256 sfController->forward( ) ../
sfFrontWebController.class.php:48 5
0.2134 4641000 sfFilterChain->execute( )
../sfController.class.php: 238 6
0.2138 4641808 sfRenderingFilter->execute( )
../ sfFilterChain.class.php:53 7
0.2138 4641808 sfFilterChain->execute( ) ../
sfRenderingFilter.class.php:33 8
0.2143 4642588 sfExecutionFilter->execute( )
../ sfFilterChain.class.php:53 9
0.2144 4643308 sfExecutionFilter->handleAction( )
../ sfExecutionFilter.class.php:42 10
0.2144 4643308 sfExecutionFilter->executeAction( )
../ sfExecutionFilter.class.php:78 11
0.2144 4643336 sfActions->execute( ) ../sfExecutionFilter.class.php: 92 12
0.2147 4644160 motoActions->executePrueba( )
../sfActions.class.php: 60 13
0.2212 5026172 MotoPeer::prueba( ) ../actions.class.php:14 14 0.2254
5285592 BaseMotoPeer::doSelect( )
../MotoPeer.php:26 15 0.2493
5756176 BaseMotoPeer::populateObjects(
) ../BaseMotoPeer.php: 241 16
0.2578 5953424 BaseMoto->hydrate( ) ../BaseMotoPeer.php:408
( ! ) Notice: Undefined offset: 2 in
/opt/lampp/htdocs/prueba/lib/
model/om/BaseMoto.php on line 185 Call
Stack
Time Memory Function Location 1 0.0008 328076
{main}( ) ../frontend_dev.php:0
2 0.1974 4333236
sfContext->dispatch( )
../frontend_dev.php:13 3 0.1974
4333264
sfFrontWebController->dispatch( )
../ sfContext.class.php:170 4
0.1981 4350256 sfController->forward( ) ../
sfFrontWebController.class.php:48 5
0.2134 4641000 sfFilterChain->execute( )
../sfController.class.php: 238 6
0.2138 4641808 sfRenderingFilter->execute( )
../ sfFilterChain.class.php:53 7
0.2138 4641808 sfFilterChain->execute( ) ../
sfRenderingFilter.class.php:33 8
0.2143 4642588 sfExecutionFilter->execute( )
../ sfFilterChain.class.php:53 9
0.2144 4643308 sfExecutionFilter->handleAction( )
../ sfExecutionFilter.class.php:42 10
0.2144 4643308 sfExecutionFilter->executeAction( )
../ sfExecutionFilter.class.php:78 11
0.2144 4643336 sfActions->execute( ) ../sfExecutionFilter.class.php: 92 12
0.2147 4644160 motoActions->executePrueba( )
../sfActions.class.php: 60 13
0.2212 5026172 MotoPeer::prueba( ) ../actions.class.php:14 14 0.2254
5285592 BaseMotoPeer::doSelect( )
../MotoPeer.php:26 15 0.2493
5756176 BaseMotoPeer::populateObjects(
) ../BaseMotoPeer.php: 241 16
0.2578 5953424 BaseMoto->hydrate( ) ../BaseMotoPeer.php:408
EDIT: the first notice appears because i didn't write "id: ~" before the fields of the model. Here it goes the lines that causes the second and the third notices:
/**
* Hydrates (populates) the object variables with values from the database resultset.
*
* An offset (0-based "start column") is specified so that objects can be hydrated
* with a subset of the columns in the resultset rows. This is needed, for example,
* for results of JOIN queries where the resultset row includes columns from two or
* more tables.
*
* #param array $row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
* #param int $startcol 0-based offset column which indicates which restultset column to start with.
* #param boolean $rehydrate Whether this object is being re-hydrated from the database.
* #return int next starting column
* #throws PropelException - Any caught Exception will be rewrapped as a PropelException.
*/
public function hydrate($row, $startcol = 0, $rehydrate = false)
{
try {
var_dump($row);
$this->id = ($row[$startcol + 0] !== null) ? (int) $row[$startcol + 0] : null;
$this->marca = ($row[$startcol + 1] !== null) ? (string) $row[$startcol + 1] : null;//184
$this->matricula = ($row[$startcol + 2] !== null) ? (int) $row[$startcol + 2] : null;//185
I think, the second and the third notices are showed because in the criteria I'm just getting one column (maatricula). But how to do it so?
Javier
If you use the "AddSelectColumn" method:
$c->addSelectColumn('MAX('.MotoPeer::MATRICULA.')');
you cannot hydrate the object - you are not selecting all the columns. Hence do not call:
MotoPeer::doSelect($c);
Instead do this:
$c->addSelectColumn('MAX('.MotoPeer::MATRICULA.')');
$stmt = MotoPeer::doSelectStmt($c);
$max_matricula = $stmt->fetchColumn(0);
Here you execute the SQL statement and fetch the single value that you're interested in into the variable $max_matricula.
Related
I am getting the error: ActiveRecord::Fixture::FixtureError: table "creatures" has no columns named "false". I have no column named false in this model.
What is going on?
Here is my fixture:
3 one:
4 name: MyString
5 no: 1
6 type1: 1
7 type2: 1
8 total: 1
9 hp: 1
10 attack: 1
11 defense: 1
12 special_attack: 1
13 special_defense: 1
14 speed: 1
15 generation: 1
16 legendary: false
17
Putting the no in single quotes solved the problem.
If I put a debugger call just before the error is raised:
[475, 484] in /usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/connection_adapters/abstract/database_statements.rb
475: fixture = fixture.stringify_keys
476:
477: unknown_columns = fixture.keys - columns.keys
478: if unknown_columns.any?
479: debugger
=> 480: raise Fixture::FixtureError, %(table "#{table_name}" has no columns named #{unknown_columns.map(&:inspect).join(', ')}.)
481: end
482:
483: columns.map do |name, column|
484: if fixture.key?(name)
(byebug) fixtures
It looks like the no got interpreted as a false:
[{"name"=>"MyString", false=>1, "type1"=>1, "type2"=>1, "total"=>1, "hp"=>1, "attack"=>1, "defense"=>1, "special_attack"=>1, "special_defense"=>1, "speed"=>1, "genneration"=>1, "legendary"=>false, "created_at"=>2023-02-05 18:53:31.63881045 UTC, "updated_at"=>2023-02-05 18:53:31.63881045 UTC, "id"=>980190962}, {"name"=>"MyString", false=>2, "type1"=>1, "type2"=>1, "total"=>1, "hp"=>1, "attack"=>1, "defense"=>1, "special_attack"=>1, "special_defense"=>1, "speed"=>1, "genneration"=>1, "legendary"=>false, "created_at"=>2023-02-05 18:53:31.63881045 UTC, "updated_at"=>2023-02-05 18:53:31.63881045 UTC, "id"=>298486374}]
Putting the no in single quotes solved the problem:
3 one:
4 name: MyString
5 'no': 1
6 type1: 1
7 type2: 1
8 total: 1
9 hp: 1
10 attack: 1
11 defense: 1
12 special_attack: 1
13 special_defense: 1
14 speed: 1
15 generation: 1
16 legendary: false
17
18 two:
19 name: MyString
20 'no': 2
21 type1: 1
22 type2: 1
23 total: 1
24 hp: 1
25 attack: 1
26 defense: 1
27 special_attack: 1
28 special_defense: 1
29 speed: 1
30 generation: 1
31 legendary: false
Define a function to implement Fibonacci Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34. Please use the function output first 20 figures of Fibonacci Sequence.
Here is a python implementation
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(5000)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
A recursive implementation
memo = [-1] * 21
memo[0] = 0
memo[1] = 1
print(memo[0], end=' ')
print(memo[1], end=' ')
def fibrec(n):
if(memo[n] == -1):
memo[n] = fibrec(n-2) + fibrec(n-1)
print(memo[n], end=' ')
return memo[n]
fibrec(20)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
I have the following text structure. The values below JTT JNX JNA JNO belong to previous line.
9 8 11 56507785 93
JTT JNX JNA JNO
76 98
9 8 60 3269557 58
9 8 53 7269558 150
JTT JNX JNA JNO
132 71 45-7705678
9 8 62 439559 82
I'd like to parse it in order to print the corresponding values in a single line like below:
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
My issue is when I use awk with FS = space (default FS) then it takes JTT as first field and JTT has 9 spaces before, so I think should be use some technique that counts how may spaces are from left until JTT JNX JNA JNO and count number of spaces from beginning until the values below JTT JNX JNA JNO in order to positionate correctly each value. I mean, 76 and 132 below JTT header, 971 below JNX, 98 below JNA and 45-7705678 below JNO.
How can this be done in awk?
$ awk --version
GNU Awk 5.0.0, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2019 Free Software Foundation.
$ uname -srv
CYGWIN_NT-6.1 3.0.7(0.338/5/3) 2019-04-30 18:08
Thanks in advance.
With GNU awk (which you have) for FIELDWIDTHS:
$ cat tst.awk
BEGIN {
OFS = ","
print "H1", "H2", "H3", "H4", "H5", "JTT", "JNX", "JNA", "JNO"
}
!NF || ($1 == "JTT") { next }
!/^ / {
if (NR>1) {
print rec
}
FS = " "
$0 = $0
$1 = $1
rec = $0
}
/^ / {
FIELDWIDTHS = "12 5 5 *"
$0 = $0
$1 = $1
for (i=1; i<=NF; i++) {
gsub(/^\s+|\s+$/,"",$i)
}
rec = rec OFS $0
}
END {
print rec
}
.
$ awk -f tst.awk file
H1,H2,H3,H4,H5,JTT,JNX,JNA,JNO
9,8,11,56507785,93,76,,98
9,8,60,3269557,58
9,8,53,7269558,150,132,71,,45-7705678
9,8,62,439559,82
$ awk -f tst.awk file | column -s, -t
H1 H2 H3 H4 H5 JTT JNX JNA JNO
9 8 11 56507785 93 76 98
9 8 60 3269557 58
9 8 53 7269558 150 132 71 45-7705678
9 8 62 439559 82
Replace OFS="," with OFS="\t" or otherwise massage to suit...
is it possible via ctags to get the function end line number as well
"ctags -x --c-kinds=f filename.c"
Above command lists the function definition start line-numbers. Wanted a way to get the function end line numbers.
Other Approaches:
awk 'NR > first && /^}$/ { print NR; exit }' first=$FIRST_LINE filename.c
This needs the code to be properly formatted
Example:
filename.c
1 #include<stdio.h>
2 #include<stdlib.h>
3 int main()
4 {
5 const char *name;
6
7 int a=0
8 printf("name");
9 printf("sssss: %s",name);
10
11 return 0;
12 }
13
14 void code()
15 {
16 printf("Code \n");
17 }
18
19 int code2()
20 {
21 printf("code2 \n");
22 return 1
23 }
24
Input: filename and the function start line no.
Example:
Input: filename.c 3
Output: 12
Input : filename.c 19
Output : 23
Is there any better/simple way of doing this ?
C/C++ parser of Universal-ctags(https://ctags.io) has end: field.
jet#localhost tmp]$ cat -n foo.c
1 int
2 main( void )
3 {
4
5 }
6
7 int
8 bar (void)
9 {
10
11 }
12
13 struct x {
14 int y;
15 };
16
[jet#localhost tmp]$ ~/var/ctags/ctags --fields=+ne -o - --sort=no foo.c
main foo.c /^main( void )$/;" f line:2 typeref:typename:int end:5
bar foo.c /^bar (void)$/;" f line:8 typeref:typename:int end:11
x foo.c /^struct x {$/;" s line:13 file: end:15
y foo.c /^ int y;$/;" m line:14 struct:x typeref:typename:int file:
awk to the rescue!
doesn't handle curly braces within comments but should handle blocks within functions, please give it a try...
$ awk -v s=3 'NR>=s && /{/ {c++}
NR>=s && /}/ && c && !--c {print NR; exit}' file
finds the matching brace for the first one after the specified start line number s.
#include<stdio.h>
#include<conio.h>
#define FIRST_PART 7
#define LAST_PART 5
#define ALL_PARTS FIRST_PART+LAST_PART
int main()
{
printf ("The Square root of all parts is %d", ALL_PARTS * ALL_PARTS) ;
getch();
return(0);
}
In the above code the FIRST_PART is defined as 7
LAST_PART is defined as 5
and ALL_PARTS is initialized as FIRST_PART+LAST_PART (which is ideally 12)
but when i am printing ALL_PARTS * ALL_PARTS is giving me 47 as the output!(But i thought answer would be 144)
Please can anyone explain me how ?
The answer should be 47
FIRST_PART + LAST_PART * FIRST_PART + LAST_PART
MULTIPLICATION HAS MORE PRECEDENCE
SO 7 + 5 * 7 + 5
7 + 35 + 5
47