Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloneRow exception #950

Open
ddmo opened this issue Nov 21, 2016 · 18 comments
Open

cloneRow exception #950

ddmo opened this issue Nov 21, 2016 · 18 comments

Comments

@ddmo
Copy link

ddmo commented Nov 21, 2016

Hi, if $templateProcessor->cloneRow doesn't find the placeholder, go in exception.
How to prevent this behaviour? I don't know if in this particular template exist the placeHolder.

I try to catch the exception, but without results...

try { $template->cloneRow('placeHolder', "aaa"); } catch (Exception $e) {}

Thanks
Stefano


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@jslirola
Copy link

jslirola commented Nov 23, 2016

Have you tried to print $e->getMessage()?

try {
	$template->cloneRow('placeHolder', "aaa"); 
} catch (Exception $e) {
	echo $e->getMessage();
}

My output: "Can not clone row, template variable not found or variable contains markup."

@ddmo
Copy link
Author

ddmo commented Nov 27, 2016

Hi @jslirola, yes I've the same output, but this is the cloneRow output, not $e->getMessage() output. I need to continue the execution if template variable not found...

Thanks
Stefano

@samael205
Copy link

Hi, You need to continue because is a loop?

@ddmo
Copy link
Author

ddmo commented Nov 29, 2016 via email

@samael205
Copy link

samael205 commented Nov 30, 2016

Continue writing your code in the catch section
try{ $template->cloneRow('placeHolder', "aaa"); } catch (Exception $e) { // do nothing... php will ignore the exception and continue } // continue coding

@ddmo
Copy link
Author

ddmo commented Nov 30, 2016 via email

@samael205
Copy link

Are you using TemplateProcessor class for the creation of the object template?.

@ddmo
Copy link
Author

ddmo commented Nov 30, 2016 via email

@samael205
Copy link

samael205 commented Nov 30, 2016

What do you want to do?. I need see some code...
P.D. Now I see that you are using templateProcessor... My Fault XD

@ddmo
Copy link
Author

ddmo commented Nov 30, 2016 via email

@ddmo
Copy link
Author

ddmo commented Nov 30, 2016 via email

@samael205
Copy link

samael205 commented Nov 30, 2016

I've already seen your mistake. You are mistakenly changing the parameters
You are passing two strings to the method cloneRow $template->cloneRow('placeHolder', "aaa");
The cloneRow Method accepts two parameters: A string and a Integer.
The string is you placeHolder, that is right.
But the next parameter is the number of clones that you want.

$template->cloneRow('placeHolder', 3);

@ddmo
Copy link
Author

ddmo commented Nov 30, 2016 via email

@samael205
Copy link

Because the setValue not thows any exception.

@ddmo
Copy link
Author

ddmo commented Nov 30, 2016 via email

@FBnil
Copy link

FBnil commented Sep 26, 2017

A solution would be using $templateProcessor->getVariables(); then use in_array() to know if you have that variable. After replacing, you can run getVariables() again, and the list would have one less element.
getVariables only shows unique variables, it does show repeated variables.

I did a pull request with the ability to disable the exception:

public function cloneRow($search, $numberOfClones = 1, $replace = true, $incrementVariables = true, $throwexception = false)

@Rappi
Copy link

Rappi commented Apr 25, 2020

Hello,

I have this Problem too.
Is there a solution?

Rappi

@detrichter
Copy link

Hello,
I'm using phpword version 0.17.0 and the behaviour of setValue and cloneRow is still as described in this thread.

Using a try catch block did not work because the exception is already caught and thrown in the cloneRow method.

So my solution is to check weather the marker / tag / variable / placeholder exists in the template like so:
$allVariables = $templateProcessor->getVariables();
if(in_array('placeHolder', $allVariables)){
$templateProcessor->cloneRow('placeHolder', $numberOfClones);
}else{
// don't clone
}

Perhaps it helps someone, even if the thread is old.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants